Use ToggleButton
and handle those drawable
through selector
.
create an XML drawable using selector
named favorite_button_image.xml
in drawable
folder and add your drawables as item
in the following XML as below...
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_action_important"
android:state_checked="true"/>
<item android:drawable="@drawable/ic_action_not_important"
android:state_checked="false"/>
</selector>
set this favorite_button_image
as a drawable in the ToggleButton
as below...and add this ToggleButton
to your Layout XML
instead of ImageView
. You will get what you wanted to do.
<ToggleButton
android:id="@+id/star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/favorite_button_image"
android:textOff=""
android:textOn="" />