/ModernAndroidPreferences

Android Preferences defined through Kotlin DSL, shown in a RecyclerView

Primary LanguageKotlinApache License 2.0Apache-2.0

ModernAndroidPreferences

GitHub release Maven Central Build status Lint status License

Android preferences in Kotlin DSL, displayed in a RecyclerView.

No XML, no troubles with PreferenceManager, Fragments, or styling, no more ListView. 🎉

Code example

// 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 app

Example Activities (with and without using ViewModel) show advanced info like back handling, saving/restoring scroll position, and using the OnScreenChangeListener.

Screenshots

Click to show

Include to project

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.

License

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.