Material Preferences
A beautiful and extensible API for bulding preferences screen.
Add this in app's build.gradle file:
implementation 'com.github.imangazalievm.material-prefs:core:<version>'
implementation 'com.github.imangazalievm.material-prefs:dialogs:<version>'
implementation 'com.github.imangazalievm.material-prefs:date-time:<version>'
implementation 'com.github.imangazalievm.material-prefs:color-picker:<version>'
Features
- Convenient and extensible DSL
- Flexible appearance settings
- Unified view on Lollipop and Pre-Lollipop
- Ability to use custom storage
- Default prefs values
- Light and dark themes
<com.imangazalievm.materialprefs.views.MaterialPreferencesViewandroid:id="@+id/prefsView"android:layout_width="match_parent"android:layout_height="match_parent" />
Step 2
Create prefs storage and provide initial values:
val defValues = defaultPrefValues {
"my_string" to "Jhon Doe"
"my_int" to 99
"my_long" to 5L
"my_float" to 2.5f
"my_boolean" to true
}
val storage = defaultPrefsStorage("my_prefs", defValues)
Step 3
Add pref items via MaterialPrefs DSL:
prefs(prefsView, storage) {
category("My category") {
preference {
title("My pref item")
icon(R.drawable.my_icon)
onClick {
//my action
}
}
}
}
Documentation
The library includes 4 modules:
- core - contains main code: simple prefs, checkbox, switch
- dialogs - dialogs prefs: single and multiple choice
- date-time - date and time pickers
- color-picker - color picker
- Three last modules based on Material Dialogs library.
preference {title("Title")summary("My description")icon(R.drawable.ic_my_icon)showArrow(true)onClick {//my action}}
switch("my_key") {title("Title")summary("My description")onChecked { isChecked ->//my action}}
checkbox("my_key") {title("Title")summary("My description")onChecked { isChecked ->//my action}}
labelPreference("my_key", String::class) {title("Title")onClick {//my action}}
abstract class MyPreference(key: String,container: PrefsContainer,private val appearanceManager: PreferencesAppearance) : BasePreference<MyPreference, MyPreferenceView, String>(container = container,key = key,appearanceManager = appearanceManager) {override fun createView(): V {}override fun loadValue(view: V) {}override fun initView(view: V) {}}
- String
- Int
- Long
- Float
- Boolean
fun myPreference(builder: PreferenceBuilder<MyPreference>) {
MyPreference(container, appearanceManager)
.apply(builder)
.also { addPreference(it) }
}
No comments:
Post a Comment