简体   繁体   中英

Change toggle button image onclick

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?

Delete both btnFav.setChecked(true) and btnFav.setChecked(false) in your OnClick method. It is a togglebutton which toggles the setChecked on its own by every click and you reset it to the old value. So in your case it always has the same value(the start value).
I would suggest you rather use setOnCheckedChangeListener instead of onClickListener .

Create a file button_toggle.xml in your res/drawable folder

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="false"
        android:drawable="@drawable/ic_slide_switch_off" />
    <item
        android:state_checked="true"
        android:drawable="@drawable/ic_slide_switch_on" />
</selector>

Try to use android:button="@drawable/favorite_btn_style" and android:background="@android:color/transparent" combination. To customize the checkbox, radio and toggle button you should use android:button instead of android:background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM