Android preferences in Kotlin DSL, displayed in a RecyclerView.
No XML, no troubles with PreferenceManager, Fragments, or styling, no more ListView. 🎉
// Setup a preference screen
val screen = screen(context) {
pref("first") {
title = "A preference"
summary = "Click me to do stuff"
click {
doStuff()
}
}
pref("second") {
title = "Another one"
iconRes = R.drawable.preference_icon_24dp
}
categoryHeader("more") {
titleRes = R.string.category_more
}
switch("toggle_feature") {
title = "Also supports switches"
}
// and many other preference widgets!
}
// Wrap the created screen in a preference adapter…
val preferencesAdapter = PreferencesAdapter(screen)
// …that can be attached to a RecyclerView
recyclerView.adapter = preferencesAdapter
Example Activities (with and without using ViewModel)
show advanced info like back handling, saving/restoring scroll position, and using the OnScreenChangeListener
.
ModernAndroidPreferences is on Maven Central, so you can get it like any other dependency:
dependencies {
implementation 'de.maxr1998:modernandroidpreferences:2.3.2'
}
NOTE: This library has previously been available as de.Maxr1998.android:modernpreferences
on Bintray JCenter,
but was migrated to Sonatype Maven Central in light of the impending JCenter sunsetting.
To get in line with Maven naming standards, it was renamed to de.maxr1998:modernandroidpreferences
.
Copyright © 2018-2021 Max Rumpf alias Maxr1998
This library is released under the Apache License version 2.0. If you use this library (or code from it) in your projects, crediting me is appreciated.
The example application however is licensed under the GNU General Public version 3, or any later version.