Skip to content

Commit

Permalink
Use Material 3 switches for switch preferences
Browse files Browse the repository at this point in the history
We have to do this explicitly since the Material 3 library doesn't have
any magic for automatically theming the switches in androidx.preference.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
  • Loading branch information
chenxiaolong committed Jul 29, 2024
1 parent 8f5d351 commit d80c7ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/src/main/res/layout/material_switch_preference.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 Andrew Gunnerson
SPDX-License-Identifier: GPL-3.0-only
-->
<!-- The ID must match the androidx preference library. -->
<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:focusable="false"
android:clickable="false"
android:background="@null" />
10 changes: 10 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
SPDX-FileCopyrightText: 2024 Andrew Gunnerson
SPDX-License-Identifier: GPL-3.0-only
-->
<resources>
<style name="SwitchPreferenceCompat.Material3" parent="Preference.SwitchPreferenceCompat.Material">
<item name="android:widgetLayout">@layout/material_switch_preference</item>
</style>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar">?attr/isLightTheme</item>
<item name="windowActionModeOverlay">true</item>
<item name="switchPreferenceCompatStyle">@style/SwitchPreferenceCompat.Material3</item>
</style>
</resources>

0 comments on commit d80c7ce

Please sign in to comment.