I know it has already been asked and answered here and here. I have tried both, but none of them is working right for me.
I have a favorite button, If it is pressed I set the item to favorite in database
and replace the image of the toggle button
, and vice versa. Here is how I am doing it:
<ToggleButton
android:id="@+id/btnFavorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:layout_marginRight="5dp"
android:background="@drawable/favorite_btn_style" />
Here is my favorite_btn_style.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/favourit_blue_btn" android:state_checked="true"/>
<!-- pressed -->
<item android:drawable="@drawable/favourit_dark_btn"/>
<!-- default/unchecked -->
</selector>
In oncreate
I check if the item is already set to favorite, then setchecked
to true
:
if (movieObj.getIsFav().intValue() == 1) {
btnFav.setChecked(true);
}
Here is my onclicklistener
on the button:
btnFav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (!btnFav.isChecked()) {
btnFav.setChecked(true);
// set favorite
dbHelper.updateMovieFavorite(movieObj.getId().intValue(), 1);
} else {
btnFav.setChecked(false);
// set favorite
dbHelper.updateMovieFavorite(movieObj.getId().intValue(), 0);
}
}
});
Function gets called, and executed fine, but no change in image.. What I am doing wrong?