ComposePrefs3 is a fully featured library of preference composables for Jetpack Compose.

Overview

ComposePrefs3

This is the M3 version of ComposePrefs. The M2 version can be found here.

ComposePrefs3 is a preferences library for Jetpack Compose which makes it easy to implement preferences/settings in your Compose Android app. Preferences are created and positioned using an intuitive and easy to use API which follows a similar design to the existing Material3 Composables. ComposePrefs3 uses DataStore to store the actual preference data.

This repository provides a sample application in which creation and use of each preference composable is demonstrated. The main settings screen can be found here .

Screenshots of this sample app

Currently supported preference composables

  • TextPref: Basic preference which only displays text and can be clicked on.
  • CheckBoxPref: Preference with a trailing CheckBox which can be (un)checked.
  • SwitchPref: Preference with a trailing Switch which can be (un)checked.
  • EditTextPref: Preference which opens a TextField in a dialog for textual user input.
  • SliderPref: Preference which displays a Slider to allow for inline numerical user input.
  • DropDownPref: Preference which shows entries in a dropdown menu and allows for user selection.
  • ListPref: Preference which opens a list of items in a dialog from which the user can select a single entry.
  • MultiSelectListPref: Preference which opens a list of items in a dialog from which the user can select multiple entries at once.

There is also support for Groups/Categories which can be created using the prefsGroup function. Usage is shown below.

Usage

First create a DataStore, for example in your MainActivity.kt:

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

Next in your actual Preferences/Settings screen, you can create a PrefScreen and pass in your DataStore as you would any other Composable:

PrefsScreen(dataStore = LocalContext.current.dataStore) {
    ..
}

Within the PrefsScreen, you can add individual preference items using prefsItem or add a group with prefsGroup:

prefsItem { TextPref(title = "Just some text", summary = "Here is some summary text") }


prefsGroup("Custom Group") {
    prefsItem { CheckBoxPref(key = "cb1", title = "Simple checkbox 1") }
    prefsItem { TextPref(title = "Just some text", summary = "Here is some summary text") }
    prefsItem { SwitchPref(key = "sw1", title = "Simple switch 1") }
}

The GroupHeader composable is used to provide the title text of each category. If you're happy with the default look you can just provide the text as the first parameter to prefsGroup.

There also exists another overload of prefsGroup where the GroupHeader can be passed in directly. Currently, the only other parameter GroupHeader supports is text color.

prefsGroup({
    GroupHeader(
        title = "Custom Group",
        color = MaterialTheme.colors.secondary
    )
}) {
    ..
}

Composables

Each preference composable excluding TextPref has a mandatory key parameter. This is the key that will be used in the DataStore and is equivalent to the key used in previous Android preference libraries.

You should be using unique keys for each preference. Using the same key for different preferences of the same type will result in their values being the same. Using the same key for different preferences of different types may result in unexpected behaviour.

TextPref

TextPref(title = "Just some text", summary = "But now with a summary")

CheckBoxPref

CheckBoxPref(
    key = "cb1",
    title = "Simple checkbox",
    summary = "But with a leading icon and summary",
    leadingIcon = { Icon(Icons.Filled.Person, "Person") }
)

SwitchPref

SwitchPref(
    key = "sw4",
    title = "Simple switch",
    summary = "But with a leading icon and summary",
    leadingIcon = { Icon(Icons.Filled.Home, "Home") }
)

EditTextPref

EditTextPref(
    key = "et1",
    title = "EditText example",
    summary = "But it has a dialog title and message",
    dialogTitle = "Dialog Title",
    dialogMessage = "Dialog Message"
)

SliderPref

SliderPref(
    key = "sp1",
    title = "Slider example with custom range and value shown on side",
    valueRange = 50f..200f,
    showValue = true
)

DropDownPref

DropDownPref(
    key = "dd1",
    title = "Dropdown with currently selected item as summary",
    useSelectedAsSummary = true,
    entries = mapOf(
        "0" to "Entry 1",
        "1" to "Entry 2",
        "2" to "Entry 3"
    )
)

ListPref

ListPref(
    key = "l1",
    title = "ListPref example",
    summary = "Opens up a dialog of options",
    entries = mapOf(
        "0" to "Entry 1",
        "1" to "Entry 2",
        "2" to "Entry 3",
        "3" to "Entry 4",
        "4" to "Entry 5"
    )
)

MultiSelectListPref

MultiSelectListPref(
    key = "msl1",
    title = "MultiSelectListPref",
    summary = "Pick multiple entries at once",
    entries = mapOf(
        "0" to "Entry 1",
        "1" to "Entry 2",
        "2" to "Entry 3",
        "3" to "Entry 4",
        "4" to "Entry 5"
    )
)

And that's it! You can create your whole preference screen in this way, and you can modify the individual parameters of each preference composable to achieve the functionality you require. If something is missing, please create an Issue so we can discuss possible solutions.

Download

In your settings.gradle file add the following

maven { url "https://jitpack.io" }

and in your module build.gradle file add the dependencies

" // Current is 1.0.0 implementation "androidx.datastore:datastore-preferences:1.0.0"">
implementation "com.github.JamalMulla:ComposePrefs3:" // Current is 1.0.0
implementation "androidx.datastore:datastore-preferences:1.0.0"
You might also like...
Jetpack Compose Boids | Flocking Insect 🐜. bird or Fish simulation using Jetpack Compose Desktop πŸš€, using Canvas API 🎨
Jetpack Compose Boids | Flocking Insect 🐜. bird or Fish simulation using Jetpack Compose Desktop πŸš€, using Canvas API 🎨

🐜 🐜 🐜 Compose flocking Ants(boids) 🐜 🐜 🐜 Jetpack compose Boids | Flocking Insect. bird or Fish simulation using Jetpack Compose Desktop πŸš€ , usi

A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.
Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Compose.
This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Compose.

JetBMICalculator This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Co

Jetpack-Compose-Demo - Instagram Profile UI using Jetpack Compose
Jetpack-Compose-Demo - Instagram Profile UI using Jetpack Compose

Jetpack-Compose-Demo Instagram Profile UI using Jetpack Compose

Jetpack-compose-animations-examples - Cool animations implemented with Jetpack compose
Jetpack-compose-animations-examples - Cool animations implemented with Jetpack compose

Jetpack-compose-animations-examples This repository consists of 4 animations: St

Compose-navigation - Set of utils to help with integrating Jetpack Compose and Jetpack's Navigation

Jetpack Compose Navigation Set of utils to help with integrating Jetpack Compose

Jetpack-compose-uis - A collection of some UIs using Jetpack Compose. built using Katalog

Jetpack Compose UIs This is a collection of some UIs using Jetpack Compose. It i

A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose
A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose

Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree

Comments
  • Update compose version to 1.3.0

    Update compose version to 1.3.0

    Versions updated:

    • Compose to 1.3.0
    • Compile SDK (forced by change of Compose version)
    • Kotlin to 1.7.20 (forced by change of Compose version)
    • Gradle and other libraries to newest possible

    This closes issue #4

    opened by newfla 1
  • Sort ListPref and MultiSelectListPref

    Sort ListPref and MultiSelectListPref

    Just a minor update to ListPref and MultiSelectListPref composable functions to accept an optional comparator to reorder items. Also, the showcase app has been updated

    opened by newfla 0
  • Bug: Lib Crash when using updated depencies

    Bug: Lib Crash when using updated depencies

    Moving from androidx.compose.material3:material3:1.0.0-alpha12 to alpha16 or higer causes the sample-app to crash on java.lang.NoSuchMethodError: No static method Checkbox(ZLkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;ZLandroidx/compose/foundation/interaction/MutableInteractionSource;Landroidx/compose/material3/CheckboxColors;Landroidx/compose/runtime/Composer;II)V in class Landroidx/compose/material3/CheckboxKt; or its super classes (declaration of 'androidx.compose.material3.CheckboxKt' appears in /data/app/~~g7IOPf0jMHbbgA_GvkNr6g==/com.jamal.composeprefs3sample-84-PSnYoYxcVUXnlKjkgMw==/base.apk) at com.jamal.composeprefs3.ui.prefs.CheckBoxPrefKt$CheckBoxPref$3.invoke(CheckBoxPref.kt:81)

    Also, switching to androidx.compose.ui:ui:>=1.3.0-beta01 causes ListPref crashes on dialog popuop

    java.lang.NoSuchMethodError: No direct method <init>(ZZLandroidx/compose/ui/window/SecureFlagPolicy;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V in class Landroidx/compose/ui/window/DialogProperties; or its super classes (declaration of 'androidx.compose.ui.window.DialogProperties' appears in /data/app/~~Crks8DClae6vNeS_sqWKTA==/com.bsoftware.orariovesuviananext-bBAfyhEJl6qDPho4KhN5CA==/base.apk) at com.jamal.composeprefs3.ui.prefs.ListPrefKt.ListPref-eNFu2v0(ListPref.kt:146)

    opened by newfla 0
  • Love it!  But a couple of questions

    Love it! But a couple of questions

    Three questions:

    • Is it possible to add any kind of validation to EditTextPref to make sure invalid data doesn't make its way into the data store? For example, I want to make sure only positive integers are added. I also want to validate one value against another (one must always be greater than the other).

    • What's the best way to show the current value of an EditTextPref? I want to make sure the current value is visible without having to click into the edit dialog.

    • Is it possible to separate the DataStore connection so that the library can provide UI only and allow for a custom ViewModel?

    Thanks - great work on this!!

    opened by DaveOddy 1
Releases(1.0.3)
  • 1.0.3(Nov 4, 2022)

    What's Changed

    • Update compose version to 1.3.0 by @newfla in https://github.com/JamalMulla/ComposePrefs3/pull/5

    New Contributors

    • @newfla made their first contribution in https://github.com/JamalMulla/ComposePrefs3/pull/5

    Full Changelog: https://github.com/JamalMulla/ComposePrefs3/compare/1.0.1...1.0.3

    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(May 22, 2022)

  • 1.0.0(May 14, 2022)

Owner
Jamal Mulla
Jamal Mulla
A flexible theme provider for Jetpack Compose. Supports dynamic theme changes and saving theme preference.

JetTheme JetTheme is a flexible theme provider for Jetpack Compose. Change the theme and recompose the UI dynamically. Save theme preference to local

Mao Yufeng 48 Oct 19, 2022
Preference functionality for Jetpack Compose

ComposePreferences Compose Preferences is a library which makes it easy to add preference functionality to your Compose app. It provides an easy to us

Niklas Schnettler 59 Jan 4, 2023
Puck - Make composables draggable with kotlin

Puck Make Composables Draggable. Including in your project Gradle Add below code

Shivam Dhuria 44 Dec 10, 2022
Sample project to demonstrate how to have clear and better interactions between composables and viewmodels.

Form Validation Sample project to demonstrate how to have clear and better interactions between composables and viewmodels. Concepts used uiState conc

Saurabh Pant 20 Dec 22, 2022
πŸš€πŸŒ†πŸ™ Display differences or animate progress between 2 images or Composables with overlay and customization options, zoom, pan gestures, and progress to observe properties for animating before-after progress

Compose Before-After Composables to display Images, or Composables as before and after composables to display differences or animate progress between

Smart Tool Factory 56 Dec 22, 2022
🧱 A tetris game fully built using Jetpack Compose

A tetris game fully built using Jetpack Compose, almost all UI elements are created by code, including the following app icon, which is also generated by Composable with @Preview.

fundroid 590 Jan 5, 2023
An Android imageboard client with the focus on maximum performance, fully written with Jetpack Compose

This project is an experimental playground to try implementing an application entirely with Jetpack Compose without using the old Android UI framework.

Dmitry 31 Dec 30, 2022
A fully functional Android app built entirely with Kotlin and Jetpack Compose

Now in Android App [Work in progress ?? ] Learn how this app was designed and built in the design case study, architecture learning journey and modula

Android 9.1k Dec 30, 2022
Fully customizable implementation of Snowfall View on Android

Android-Snowfall Fully customizable implementation of "Snowfall View" on Android. That's how we use it in our app Hotellook Compatibility This library

Daniel Jette 0 Dec 4, 2021
laboratory is the next generation Minecraft server management tool fully written in Kotlin

laboratory laboratory is the next generation Minecraft server management tool fully written in Kotlin Installation Linux: Clone this repository using

mooz 5 Oct 29, 2022