Search code examples
androidandroid-layoutandroid-switch

Change Switch color in Navigation Drawer menu item


I'm having trouble changing the color of my Navigation drawer switch items. I want each one to have a custom color associated with it's action.

I've defined my nav drawer with a menu like this:

<item
    android:title="@string/nav_drawer_services_title">
    <menu>
        <group android:checkableBehavior="none">
            <item
                android:id="@+id/Red_selector"
                android:title="@string/nav_drawer_red_title"
                app:actionLayout="@layout/switch_layout"/>
            <item
                android:id="@+id/Blue_selector"
                android:title="@string/nav_drawer_blue_title"
                app:actionLayout="@layout/switch_layout"/>
        </group>
    </menu>
</item>

My switch_layout.xml is like this:

<Switch
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:onClick="toggleService"
android:checkable="true"
android:checked="true"
style="@style/MyCustomSwitchTheme"
/>

I've tried changing MyCustomSwitchTheme as suggessted here: Change "on" color of a Switch, yet the switch items remain the same color.

Why doesn't my custom theme style override the default? I can't seem to change these switches from the colorAccent defined in my colors.xml and AppTheme.

Anyone know a way around this?

Edit: Here is the styles code, sorry about not including that with the OP.

<style name="MyCustomSwitchTheme">
    <!-- active thumb & track color (30% transparency) -->
    <item name="colorControlActivated">#46bdbf</item>

    <!-- inactive thumb color -->
    <item name="colorSwitchThumbNormal">@color/light_gray_color</item>

    <!-- inactive track color (30% transparency) -->
    <item name="android:colorForeground">#42221f1f</item>
</style>

Solution

  • just use android:theme="@style/MyCustomSwitchTheme"

    instead style="@style/MyCustomSwitchTheme"