Build a Settings Screen via Code
I mentioned that a code-built Settings screen was possible back in Add the Preferences Resource File, and this feels like a perfect time to create one. Before we do anything, though, we need to get the Preferences dependency in here:
| implementation "androidx.preference:preference-ktx:$preference_version" |
After adding the version variable and syncing, we want a new class in a new settings package called SettingsFragment.
Create the SettingsFragment Class
As we did before, this will be a child class of the PreferenceFragmentCompat class and include an onCreatePreferences function:
| class SettingsFragment : PreferenceFragmentCompat() { |
| |
| override fun onCreatePreferences( |
| savedInstanceState: Bundle?, ... |
Get Kotlin and Android Development featuring Jetpack now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.