Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change switch color when disabled

I've a switch that when is enabled and checked, it's color is my colorPrimary.

I want to have the same color when it's checked but disabled, and i can't find a way to get it done.

I tried using a selector, but it changes the switch background and not the toggle itself.

How can i change the toggle of the switch color?

Thanks.

like image 436
Sharas Avatar asked Dec 08 '22 23:12

Sharas


1 Answers

1-use this in styles.xml

 <style name="SwitchStyle" parent="Theme.AppCompat.Light">
      <item name="colorControlActivated">@color/theme</item>
      <item name="colorSwitchThumbNormal">@color/grey300</item>
      <item name="android:colorForeground">@color/grey600</item>
</style>

and then this for your switch:

<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/SwitchStyle" />

2-Switch:

<Switch
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector.xml" />

selector.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@drawable/on" android:state_checked="true"/> 
     <item android:drawable="@drawable/off" android:state_checked="false"/>
 </selector>
like image 109
Amir_P Avatar answered Dec 10 '22 13:12

Amir_P