6

So i'm updating my apps to fully support Monet and Material You guidelines, and the official site mentions a new design for the switches. I used it, and that's the result:

MaterialSwitch in the app

I have a preference screen using Androidx preferences library, latest version available at the time of writing, and the only way i found to theme the switches (except the manual theming, which makes no sense) is to use this line in the app's theme:

<item name="switchStyle">@style/Widget.Material3.CompoundButton.MaterialSwitch</item>

And using SwitchPreferenceCompat (it doesn't work in the regular SwitchPreference) this is what i get:

Switch in preference

Regardless of the width (which is different, but can be changed) the disabled state is completely different and doesn't match the rest of the app. Why? and most importantly, why do they suggest to use a library which:

  1. Doesn't support Material You out of the box
  2. Doesn't support any new Material3 component
  3. It's hard to properly customize in general

?

I don't want to be too critical, but this is out of my understanding.

EDIT: at the moment, i'm using switchCompat everywhere, to make the app uniform. Looking at the system apps, i can find 4 different type of switches: a custom switch similar to the second screenshot, the old one and the two types in this question. That's hella confusing.

m.i.n.a.r.
  • 922
  • 2
  • 12
  • 28
  • 2
    I cannot comment due to low karma, but I just wanted to let you know I have filed a bug report to Google about this on their IssueTracker: https://issuetracker.google.com/issues/247430819 It's been assigned, so hopefully it will get fixed soon. – thebluepandabear Sep 20 '22 at 21:47

2 Answers2

17

I understand Google stance on this, they don't want to make androidx.* packages dependent to Material library itself, maybe they should provide a separate preference package but this time with fully Material widgets.

In order to have the brand new MaterialSwitch of Material 1.7.0 with preference, I've overridden its widgetLayout with a custom layout by android:widgetLayout="@layout/preference_material_switch" (in fact I applied that programmatically like .widgetLayoutResource = R.layout.preference_material_switch) and put the following on preference_material_switch.xml layout file,

<?xml version="1.0" encoding="utf-8"?>
<!-- Derived from https://github.com/androidx/androidx/blob/8cb282cc/preference/preference/res/layout/preference_widget_switch_compat.xml -->
<com.google.android.material.materialswitch.MaterialSwitch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/switchWidget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:clickable="false"
    android:focusable="false" />

Update: Alternative solution rather than adding android:widgetLayout= to each switch is to add the following to your base theme,

         <item name="preferenceTheme">@style/AppPreferenceThemeOverlay</item>

Then adding the following styles also,

    <style name="AppPreferenceThemeOverlay" parent="@style/PreferenceThemeOverlay">
        <item name="switchPreferenceCompatStyle">@style/AppSwitchPreference</item>
    </style>

    <style name="AppSwitchPreference" parent="@style/Preference.SwitchPreferenceCompat.Material">
        <item name="widgetLayout">@layout/preference_material_switch</item>
    </style>

And here is the result,

enter image description here

Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81
3

The same problem, After seeing these replies, I'm thinking to build the settings fragment without androidx.preference.

MKevin3 said:

I hate the provided Android preferences setup and look. So many times they changed the rules and broke what I had.

Not that this helps you probably but I just did my own to avoid all the headaches and I am in full control of the look. If push comes to hove you might consider doing this as well instead of fighting the "Android Way".

sc00ty said:

I gave up trying to use their widgets and fragments. It was so much less of a headache to spend a little time making my own compound widgets for each setting type.

Taha Sami
  • 1,565
  • 1
  • 16
  • 43
  • That's true, but in this particular case we also have a difference between the material3 switch and the material3 components switch, independently from the library. And, again, that's terribly confusing – m.i.n.a.r. Sep 08 '22 at 21:56