Search code examples
androidandroid-layoutandroid-checkbox

Selector doesn't work on Checkbox with vectors


So, I've done selectors in the past and I have no idea why it doesn't work this time. I have 2 vector drawables and the plan is to change between them on click. I've created the following selector file:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/ic_vector_favorite"
          android:state_checked="true"
          android:state_selected="true"/>

    <item android:drawable="@drawable/ic_vector_favorite_border"
          android:state_checked="false"/>
</selector>

And this is my checkbox:

<CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/ic_phone_margin"
            android:button="@drawable/ic_favorite_selector"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/iv_call"
            app:layout_constraintTop_toTopOf="parent"/>

But, whenever I click it, nothing changes. I've tried with android:checked="true" but it's still the same.


Solution

  • You just need to remove android:state_selected="true" from your selector. It should just be

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/ic_vector_favorite"
              android:state_checked="true"/>
    
        <item android:drawable="@drawable/ic_vector_favorite_border"
              android:state_checked="false"/>
    </selector>