Android - Set Click focus on button

I have 4 buttons in my layout, now when I press any button, after the click event, the button should be high, indicating that it was last pressed.

To create such a thing, I tried the following:

the code:

btn1.setOnClickListener(new button_click_listener()); btn2.setOnClickListener(new button_click_listener()); class button_click_listener implements Button.OnClickListener { @Override public void onClick(View v) { if(v==btn1) { btn1.requestFocus(); } if(v==btn2) { btn2.requestFocus(); } ....... } } 

XML layout:

 <Button android:text="Click 1" android:id="@+id/btnClick1" android:layout_width="70dp" android:layout_height="wrap_content" style="@android:style/Widget.Button.Small" android:padding="10dp" android:focusableInTouchMode="true"> </Button> 

How to show the click on the button that was last pressed? Please show me the way and give advice.

Update:

If I set android:focusable="true" , then the button will be highlighted and focused, but at the same time I need to double-click the button to execute the actual click event.

+6
android button
source share
3 answers

its a simple buddy ... you just need to set the backgroundcolor of the ur button to the ur click event, and every time any button is clicked, you need to set other background colors of the buttons null

+3
source share

This function is similar to RadioButtonGroup, it adjusts RadioButtonGroup to your requirements.

Style setting in RadioGroup:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:state_window_focused="false" android:drawable="@drawable/radio_hover" /> <item android:state_checked="false" android:state_window_focused="false" android:drawable="@drawable/radio_normal" /> <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/radio_active" /> <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/radio_active" /> <item android:state_checked="true" android:state_focused="true" android:drawable="@drawable/radio_hover" /> <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/radio_normal_off" /> <item android:state_checked="false" android:drawable="@drawable/radio_normal" /> <item android:state_checked="true" android:drawable="@drawable/radio_hover" /> </selector> 

Set this switch to your radio button.

0
source share

The problem is that you need a new state. Follow the instructions here and add "state_highlighted" or whatever you would like to name:

How to add custom button state

0
source share

All Articles