Android is another rollover image on ImageButton

Is it possible to specify a different image when user focus falls into ImageButton? I want to display the image button on LinearLayout and change the image when user focus enters the button or when the user clicks the button.

Thanks.

+6
android imagebutton
source share
2 answers

Yes you can do it. You need a flexible XML file that defines a selector.

  <selector xmlns: android ...
   <item android: state_enabled = "false" android: state_focused = "true" android: drawable = "..." />
   <item android: state_enabled = "true" android: state_focused = "false" android: drawable = "..." />
 </selector>

Then use the identifier of this retrieved XML when specifying ImageButton in your XML format.

+5
source share

The precedent answer did not work for me. Here is the code I found somewhere else:

<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/refresh_pushed" android:state_pressed="true" /> <item android:drawable="@drawable/refresh" /> </selector> 

You can also add state for objects with focus by adding a line and using:

Android: state_focused = "true"

0
source share

All Articles