5

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jyclop
  • 469
  • 1
  • 6
  • 15

2 Answers2

10

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" />
Ali
  • 839
  • 11
  • 21
  • Thanks got me on the right track! I ended up just setting all 3 of those colors to `@color/colorPrimary` to make it truly the same color on and off. Thanks for your help! – Jyclop Jul 11 '17 at 03:43
1

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Cochi
  • 2,199
  • 2
  • 12
  • 15