Reputation: 499
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?
Upvotes: 6
Views: 2350
Reputation: 2209
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.
Upvotes: 1
Reputation: 859
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" />
Upvotes: 11