Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 505
Jyclop Avatar asked Jul 11 '17 01:07

Jyclop


People also ask

What is colorControlActivated?

attr/colorControlActivated The color applied to icons/controls in their activated state (e.g. checked).


2 Answers

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" />
like image 195
Ali Avatar answered Nov 14 '22 03:11

Ali


You can use the SwitchCompat component with a custom style:

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

In your res/value/style.xml file define the style

<style name="SwitchStyle">
    <item name="colorSwitchThumbNormal">@color/color_switch_off</item>
    <item name="colorControlActivated">@color/color_switch_on</item>
</style>

Hope this helps.

like image 1
Cochi Avatar answered Nov 14 '22 02:11

Cochi