Yury Pogrebnyak
Yury Pogrebnyak

Reputation: 4173

Android: highlight button until other button is pressed

I have 4 buttons in my layout - they are switchers between 4 possible contents of View. Any time one of these buttons is pressed I need to highlight it (simply load other background, other image and change the color of the text). Trouble is, I need this to be done from the moment button is touched (not AFTER the click) and until other button is pressed. Simple use of onClick() methods won't be very good because I'd have to check if other buttons are already highlighted, and also changes will be made only AFTER the click. Any good solution for that?

Upvotes: 0

Views: 1717

Answers (1)

Barak
Barak

Reputation: 16393

How about an onFocusChange listener?

button.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // set the background, image and color for focused
        } else {
            // set the background, image and color back to normnal
        }
    }
});

Set one of those for each button and you should be all set.

Upvotes: 1

Related Questions