Search code examples
androidtogglebutton

Change "on" color of a Switch


I'm using a standard Switch control with the holo.light theme in a ICS app.

I want to change the highlighted or on state color of the Toggle Button from the standard light blue to green.

This should be easy, but I can't seem to work out how to do it.


Solution

  • As of now it is better to use SwitchCompat from the AppCompat.v7 library. You can then use simple styling to change the color of your components.

    values/themes.xml:
    
    <style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">@color/my_awesome_color</item>
    
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">@color/my_awesome_darker_color</item>
    
        <!-- colorAccent is used as the default value for colorControlActivated,
             which is used to tint widgets -->
        <item name="colorAccent">@color/accent</item>
    
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight, and colorSwitchThumbNormal. -->
    
    </style>
    

    ref: Android Developers Blog

    EDIT:

    The way in which it should be correctly applied is through android:theme="@style/Theme.MyTheme" and also this can be applied to parent styles such as EditTexts, RadioButtons, Switches, CheckBoxes and ProgressBars:

    <style name="My.Widget.ProgressBar" parent="Widget.AppCompat.ProgressBar">
    
    <style name="My.Widget.Checkbox" parent="Widget.AppCompat.CompoundButton.CheckBox">