Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Text ON/OFF Color to Switch Android

I have a custom selector to Thumb and Track Switch

switch_thumb_selector.xml

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

switch_track_selector.xml

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

I want to get this Switch like result and put text ON/OFF

Switch

My question is how can i change the color of the ON / OFF text without changing the thumb image? The color ON / OFF text is white and the image color of the thumb is same. It confuses the two colors

like image 976
Are0215 Avatar asked Oct 23 '25 15:10

Are0215


1 Answers

You can set the text color to a color state list selector. Use the same states as before, but use android:color instead of android:drawable as the value for each state. Color StateList

Notice that this resource goes in res/color, not res/drawable

res/color/switch_text_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#00000000"  android:state_checked="true" />
    <item android:color="#FFFFFFFF"  />        
</selector>
like image 126
dangVarmit Avatar answered Oct 25 '25 04:10

dangVarmit