Home > Net >  Switch thumb can't set state_enabled in selector
Switch thumb can't set state_enabled in selector

Time:07-07

I'm making a custom switch with my own thumb and track. What I want is, in my thumb xml, set a different color in checked state, unchecked state and also, set a different color when the switch is disabled. The problem is that when I set state_enabled to false, nothind happen, just checked states works, why is this happening?

<?xml version="1.0" encoding ="utf-8"?>
<selector xmlns:android.....>
   <item android:state_checked = "true">
      <shape android:shape = "oval">
         <size android:width = "24dp" android:height="24dp"/>
         <solid android:color="@color/green"/>
      </shape>
   </item>

   <item android:state_checked="false">
      <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/red"/>
      </shape>
   </item>
   <item android:state_enabled="false">
       <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/black"/>
       </shape>
   </item>
</selector>

CodePudding user response:

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

<item android:state_enabled="true" android:state_selected="true">
<shape android:shape = "oval">
         <size android:width = "24dp" android:height="24dp"/>
         <solid android:color="@color/green"/>
      </shape>
    </item>

<item android:state_enabled="true" android:state_selected="false">
<shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/red"/>
      </shape>
</item>
<item android:state_enabled="false">
 <shape android:shape="oval">
          <size android:width = "24dp" android:height="24dp"/>
          <solid android:color="@color/black"/>
       </shape>
</item>

</selector>

Try this. There is similar solution in this link. https://stackoverflow.com/a/12383390/6183169

  • Related