androidandroidxandroid-preferencesswitchpreference

SwitchPreferenceCompat wrong "off" color on real device


I'm using SwitchPreferenceCompat from androidx.preference:preference:1.2.0. In Android Studio preview everything looks good:

enter image description here

But in real device it look this:

enter image description here

Here my preferences.xml:

<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory app:title="@string/auth_header">

        <SwitchPreferenceCompat
            app:key="@string/auth_key"
            app:title="@string/auth_title"
            android:icon="@drawable/ic_fingerprint"/>

    </PreferenceCategory>

</PreferenceScreen>

And app theme:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Notes" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/color_primary</item>
        <item name="colorPrimaryVariant">@color/color_primary_variant</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/color_secondary</item>
        <item name="colorSecondaryVariant">@color/color_secondary_variant</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">@android:color/transparent</item>
        <!-- Customize your theme here. -->
        <item name="android:windowLightStatusBar">true</item>
        <item name="windowActionModeOverlay">true</item>
        <item name="colorAccent">@color/black</item>
    </style>
</resources>

Note that colorAccent is not related to this problem. Without it the result is the same (even if you remove all values from the theme, the result will not change). I use it just to set the preference header color.

Question: Is this a library bug or am I doing something wrong?


Device: POCO X3 NFC (MIUI 12.5, Android 11).

androidx.preference:preference:1.2.0

com.google.android.material:material:1.8.0


Solution

  • This is probably a bug with the SwitchCompat widget related to the Material3 theme.

    I opened issues here:

    https://issuetracker.google.com/issues/271871302

    https://github.com/material-components/material-components-android/issues/3295


    I ended up with own settings implementation (regular fragment + layout with MaterialSwitch) because it's easier and faster implement own solution than dealing with these bugs.

    enter image description here

    It also adds more customization, but now I have to update settings manually (I use a regular ViewModel to keep the state and my own PreferencesRepository to save/load preferences).