I have a Button in my android application, and I need to expand it on click and display a Seekbar inside it, like at the picture below. What is the best practice to do that?
Use below code to handle tap of button to set visiblility of seekbar invisible if visible or visible if invisible.
Button click = (Button) findViewById(R.id.but);
Seekbar seek = (Seekbar) findViewById(R.id.seek);
click.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (seek.getVisibility() == View.VISIBLE) {
seek.setVisibility(View.INVISIBLE);
} else {
seek.setVisibility(View.VISIBLE);
}
}
});
Related
I have a small button and when clicked it will expand to width:match_parent. When clicked again I want the button parameters from the xml to be applied. Its a custom button so its not as easy as just resizing the width.
My current code looks like:
btnBottomSheetTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
mSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
btnBottomSheetTitle.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, 150));
btnBottomSheetTitle.setCornerRadius(0);
} else {
mSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
});
If You have style for button You can set it programmatically. Before setting setOnClickListener save current button's LayoutParams
final android.view.ViewGroup.LayoutParams savedParams = btnBottomSheetTitle.getLayoutParams();
In Your else add:
btnBottomSheetTitle.setBackgroundResource(R.drawable.your_style);
btnBottomSheetTitle.setLayoutParams(savedParams);
I have a custom dialog box which consist of some text and a "next" button. Now, If my application finishes and ends, then the same custom dialog box appears but I need to change the button text with "close" and so the respective action. Here is the code:
private void CustomizedDialog(String text1, String text2) {
final Dialog customDialog = new Dialog(this);
customDialog.getWindow();
customDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
customDialog.setContentView(R.layout.custom_dialog);
TextView firstTextView = (TextView)customDialog.findViewById(R.id.textView1);
firstTextView.setText(text1);
TextView secondTextView = (TextView) customDialog.findViewById(R.id.textView2);
secondTextView.setText(text2);
if (currentInfo == (information.size() - 1)) {
View closeButton = customDialog.findViewById(R.id.answer_next_button);
closeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customDialog.dismiss();
}
});
} else {
View nextButton = customDialog.findViewById(R.id.answer_next_button);
nextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("next button", "next button clicked");
customDialog.dismiss();
if (currentInfo < (information.size() - 1)) {
currentInfo++;
nextInformation();
}
}
});
}
customDialog.show();
}
How can i change next button and its associated action in the custom dialog box with the close button and its action when the application reaches its end? Any help would be really appreciated. Thanks :)
Assuming your conditionals are ok, you should access the first condition if it's the last entry, to change the text you simply need to cast the View to a Button and call the .setText(String t) method:
Button closeButton = (Button)customDialog.findViewById(R.id.answer_next_button);
closeButton.setText("Close");
As for the associated action it all seems correct, just add any other actions inside the onClick method of the first condition.
I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
}
}//end void onClick
});//end test button on click listener
try
testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));
However ToggleButton might suit your case better.
As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.
I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:
final Drawable first = getResources().getDrawable(
android.R.drawable.arrow_up_float);
final Drawable second = getResources().getDrawable(
android.R.drawable.arrow_down_float);
final Button testButton = (Button) findViewById(R.id.toggleButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (testButton.getBackground().equals(first)) {
testButton.setBackgroundDrawable(second);
} else {
testButton.setBackgroundDrawable(first);
}
}
});
as the other friends answered , it is preferable to use the ToggleButton in Android ,
and in your case, if you want to keep your code , so your method should be like this :
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});//end test button on click listener
You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.
You can use Buttons or Image buttons..
private ImageButton mod1,mod2;
mod1 = (ImageButton) findViewById(R.id.mod1);
mod2 = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);
public void onClick(View v) {
mod1.getDrawable().clearColorFilter();
mod2.getDrawable().clearColorFilter();
switch (v.getId()) {
case R.id.mod1:
mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
case R.id.mod2:
mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
}
}
I'm having a button in a sliding drawer in a Android Application. The problem is it does not seem to react to any clicks as normal buttons do.
I'm guessing the problem is that it's a different view than buttons on the normal view.
If I implement a button the normal way like this
myAgenda = (Button)findViewById(R.id.BtnMyAgenda);
myAgenda.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.BtnMyAgenda:
test.setAnimation(leftLeft);
test.startAnimation(leftLeft);
break;
}
I'm guessing there is something wrong with the above code since the button is in a SlidingDrawer and not in the "normal" view.
Any ideas how to fix the problem?
Here is the code
Register with event listner like below code
button.setOnClickListener(clickButtonListener);
and create this listner for button
private OnClickListener clickButtonListener= new OnClickListener()
{
#Override
public void onClick(View v)
{
if(v == button)
{
}
}
}
I actually found the solution to the problem, I simply created a new view.onclicklistener specific to that button.
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
Can I make ImageView in android make some action when I click on that ImageView ?
For example, when I click on ImageView imgV that some panel be visible and when I click on other ImageView imgUV that he become unvisible .
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
int i=tbrSearchNear.VISIBLE;
tbrSearchNear.setVisibility(1-i);
tbrSearchCriterium.setVisibility(i);
}
});
It looks like that doesn't register click at all .
Maybe ImageButton is what you are looking for? http://developer.android.com/reference/android/widget/ImageButton.html
Setting visbility to 1-View.VISIBLE translates to 1-0; if you want to hide it, write something like:
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
// toggle visibility
int visibility=tbrSearchNear.getVisbility() == View.VISIBLE ? View.GONE : View.VISIBLE;
tbrSearchCriterium.setVisibility(tbrSearchNear.getVisbility());
tbrSearchNear.setVisibility(visbility);
}
});