I know this is a question that has been asked many times before, but I can't seem to solve it in my code. I have two buttons, and when one is pressed, I would like to keep it in its selected state, and vice versa. I have tried doing it using but the setSelected and setPressed, but I can't seem to get it to work. Here is the code I am using:
region_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
objects = category;
adap.notifyDataSetChanged();
proximity_button.setPressed(false);
region_button.setPressed(true);
}
});
proximity_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
objects = proximity;
adap.notifyDataSetChanged();
region_button.setPressed(false);
proximity_button.setPressed(true);
}
});
Edit: Based on the comments, I need to add that I have a custom xml background for the buttons, and would like to retain the current look.
I searched on Google and found this Stack Overflow post:
How can i keep one button as pressed after click on it?
mycodes_Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mycodes_Button.setPressed(true);
return true;
}
});
But read the comment, it's pretty interesting!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With