I have in my layout a button, which is has to change image when pressed. I Have a selector xml. Here is button:
<ImageButton
android:id="@+id/cwcfwdvcs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:layout_marginTop="16dp"
android:background="@drawable/button_add_to_favorites_unactive"
android:drawable="@drawable/add_to_favorites"
android:text="Favotites"/>
Here is selector add_to_favorites.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_add_to_favorites_active"
android:state_checked="true" />
<item android:drawable="@drawable/button_add_to_favorites_unactive" />
You should use the button selector state like the following code i.e(res/drawable/button.xml)