How to apply custom style to all switches using switchStyle attribute

Viewed 212

I know that we can simply achieve this using the colorControl attributes and apply the style using the theme attribute in the view. For example,

<style name="MySwitch">
    <item name="colorControlNormal">@color/grey</item>
    <item name="colorControlActiviated">@color/primaryColor</item>
</style>

<Switch
    ...
    app:theme="@style/MySwitch"/>

But in this scenario we need to apply theme attribute to all switches in the activity or the app. Instead, I want to apply this to the whole app theme. So, I tried achieving the same doing these ways but none of it worked at all.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryDarkColor</item>
    <item name="colorAccent">@color/secondaryColor</item>
    <item name="switchStyle">@style/MySwitch</item>
</style>

Method 1:

<style name="MySwitch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="buttonTint">@color/primaryColor</item>
</style>

switch_thumb:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primaryColor" android:state_checked="true" />
    <item android:color="@android:color/darker_gray" />
</selector>

switch_track:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/primaryLightColor" android:state_checked="true" />
    <item android:color="@android:color/darker_gray" />
</selector>

Method 2:

<style name="MySwitch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="thumbTint">@drawable/switch_thumb</item>
    <item name="trackTint">@drawable/switch_track</item>
</style>

Method 3:

<style name="MySwitch" parent="Widget.AppCompat.CompoundButton.Switch">
    <item name="thumb">@drawable/switch_thumb</item>
    <item name="track">@drawable/switch_track</item>
</style>

I have tried to find other solutions but they don't contain the information on applying the style to whole theme like I want.

0 Answers
Related