Issue
Android 12's settings' switches now look like this,
which its code is here:
but neither androidx.preference.SwitchPreferenceCompat
nor androidx.preference.SwitchPreference
look like this in my app with updated material library (1.5.0, Material 3),
How can I have the same looking (and feeling, animation, etc) switches at least in Android 12 or how Android is doing it for itself? Thanks!
Solution
You can easily customize your switches to look like these.
In styles.xml
add this :
<style name="App.Switches" parent="Widget.Material3.CompoundButton.Switch">
<item name="track">@drawable/switch_track</item>
<item name="android:thumb">@drawable/switch_thumb</item>
</style>
switch_track.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="56dp" />
<size
android:width="64dp"
android:height="28dp" />
</shape>
</item>
</layer-list>
switch_thumb.xml
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<shape android:shape="oval">
<solid android:color="#000000" />
<size
android:width="20dp"
android:height="20dp" />
</shape>
</item>
</layer-list>
In the end add this line in your app's main theme to apply this style to all the switches in your app :
<item name="switchStyle">@style/App.Switches</item>
Answered By - DrHowdyDoo
Answer Checked By - Willingham (JavaFixing Volunteer)