Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep android button selected state

Tags:

android

button

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.

like image 215
coder Avatar asked Feb 16 '12 20:02

coder


1 Answers

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!

like image 173
Cehm Avatar answered Sep 19 '22 23:09

Cehm