I am trying to implement toggle functionality on a star button in Android. This is my imagebutton in res/my:
<ImageButton
android:id="@+id/star_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/star"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:onClick="onToggleStar"
android:background="#00ffffff"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:padding="20dp"/>
This is drawable/star.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:drawable/btn_star"/> <!-- pressed -->
<item android:drawable="@android:drawable/btn_star_big_off"/>
</selector>
This is my onclick handler:
public void onToggleStar(View view)
{
view.setSelected(!view.isSelected());
}
the problem I am facing is button src is always taking default value i.e. btn_star_big_off
This question has been asked before here Android ImageButton with a selected state? and some other places as well but I am not able to figure out any problem.
Edit
I have tried this as well
<Button
android:id="@+id/star_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/star"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:onClick="onToggleStar"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:padding="20dp"/>
with other two functions same. I am using genymotion emulator.