According to this question I am trying to create a custom layout and add it to my root PreferenceScreen XML, but I am facing some problems, first, the switch_preference_layout.xml
looks corrupted when I add it inside the Preference tag
here's my custom layout for the switch to dark mode example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@android:id/widget_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/_8sdp"
android:weightSum="2">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:id="@android:id/title"
style="@style/TextAppearance.PreferenceTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Dark Mode"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@android:id/summary"
style="@style/TextAppearance.PreferenceSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="Improve visibility and save energy" />
</LinearLayout>
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1" />
</LinearLayout>
and it looks like the following
after adding it in root_prefernces.xml using android:widgetLayout="@layout/switch_preference_layout"
it looks like that
it's almost invisible! , I tried also to use android:layout="@layout/switch_preference_layout"
and it completely disappeared
the full code of root_prefernces.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<PreferenceCategory>
<Preference
android:defaultValue="false"
android:key="switchToDarkMode"
android:widgetLayout="@layout/switch_preference_layout">
</Preference>
</PreferenceCategory>
<Preference
android:title="Publisher info"
app:key="aboutPublisher">
</Preference>
<Preference
android:title="@string/about"
app:key="@string/about">
</Preference>
</PreferenceScreen>
Second Problem
When I try to extend `Preference` in `SettingsFragment` I see working and it's required to pass `context` in its constructor unlike the other `Preference` class in this [answer][5]and it also needs to override some methods and the last one isVisible
the android studio shows working message 'isVisible' in 'PreferenceFragmentCompat' is final and cannot be overridden
and if I removed it's showing it's required to override it, I am not sure but it looks like a bug