If I was using an ImageButton with a selector for its background, is there a state I can change which will make it change its appearance? Right now I can get it to change images when pressed, but there seems to be no "highlighted" or "selected" or similar state which lets me toggle its appearance at will.
Here's my XML; it only changes appearance when pressed.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/map_toolbar_details_selected" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/map_toolbar_details_selected" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/map_toolbar_details_selected" />
<item android:drawable="@drawable/map_toolbar_details" />
This works for me:
And then in java:
For smooth transition you can also mention animation time:
Try this:
Also for colors i had success with
Create an XML-file in a
res/drawable
folder. For instance, "btn_image.xml":You can combine those files you like, for instance, change "bg_state_1" to "bg_state_deselected" and "bg_state_2" to "bg_state_selected".
In any of those files you can write something like:
Create in a layout file an ImageView or ImageButton with the following attributes:
Later in code:
ToggleImageButton
which implementsCheckable
interface and supportsOnCheckedChangeListener
andandroid:checked
xml attribute:res/values/attrs.xml:
The best way to do this without more images :