I want to support at least api 10, I want to be able to style my preferences nicely, I want to be able to have headers (or to show PreferenceScreen
s). It seems
There is a good tutorial and Github project that explains in detail how to make a custom preference class that extends the Support Preference library:
https://medium.com/@JakobUlbrich/building-a-settings-screen-for-android-part-3-ae9793fd31ec -- "Building an Android Settings Screen (Part 3)", by Jakob Ulbrich
https://github.com/jakobulbrich/preferences-demo -- Sample Android project on Github
The key points are:
You will need a custom DialogPreference
or ListPreference
, which controls how the preference row looks and functions. (It can also contain a reference to a layout that should display in the launched dialog). Add this DialogPreference
to your XML preference file.
You will need a custom PreferenceDialogFragmentCompat
, which controls the launching of the Dialog when the preference row is clicked. You can configure the Dialog's view in onBindDialogView()
.
In your preference screen which extends PreferenceFragmentCompat, override onDisplayPreferenceDialog()
to launch your custom PreferenceDialogFragmentCompat
.
You must only extend the support classes, not the platform classes. For example, extend androidx.preference.EditTextPreference
instead of android.preference.EditTextPreference