Search code examples
androidandroid-viewandroid-switch

Make switch stay same color when on and off


I'm trying to make an Android Switch act as just a selection between two options, so I want to make it so that the switch is the same color when it's 'on' as when it's 'off'. How do I do this?


Solution

  • Add this to Styles.xml:

    <style name="SelectionSwitch" parent="Theme.AppCompat.Light">
        <!-- active thumb & track color (30% transparency) -->
        <item name="colorControlActivated">#f1f1f1</item>
    
        <!-- inactive thumb color -->
        <item name="colorSwitchThumbNormal">#f1f1f1
        </item>
    
        <!-- inactive track color (30% transparency) -->
        <item name="android:colorForeground">#42221f1f
        </item>
    </style>
    

    and add the Switch to to your layout as below:

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