Search code examples
androidbuttonswitch-statementtextcolor

Android Switch button color not changing in 4.0.4


I have used Switch button, everything works fine when the app run it on 4.2 and above, but the text color not at all changed to white in 4.0.4 i have tried all possible solution

My Switch :

<Switch
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/facilityassetdescription"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:background="@drawable/offbuttonbg"
        android:textColor="@style/toggle_text"
        android:textOff="OFF"
        android:textOn="ON"
        android:thumb="@drawable/switchselector" />

My style file

<style name="toggle_text">
        <item name="android:textColor">@color/toggle</item>
    </style>

res/color/toggle_text.xml

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

    <item android:state_checked="true" android:color="#ffffff"/>
    <!-- Default State -->
    <item android:color="#ffffff"/>

</selector>

Kindly give any idea to get rid of this problem


Solution

  • Use below style:

    <style name="toggle_text" parent="@android:style/TextAppearance.Small">
        <item name="android:textColor">@color/toggle</item>
    </style>
    

    And, In xml file for switch mention android:switchTextAppearance attribute instead of using android:textColor:

    <Switch
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/facilityassetdescription"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="7dp"
        android:background="@drawable/offbuttonbg"
        android:switchTextAppearance="@style/toggle_text"
        android:textOff="OFF"
        android:textOn="ON"
        android:thumb="@drawable/switchselector" />