Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a preference screen with Android Jetpack Compose

i have to build a new Android app. Since Jetpack Compose is now stable i want to build the entire UI with that. Further i need also a Preference/Settings screen where the user can specify his preferences. According the doc it is still recommended doing this via Fragments. https://developer.android.com/guide/topics/ui/settings

I found also an external library witch would provide this functionality in the compose way. https://github.com/alorma/Compose-Settings

Has anybody done this before? Whats the "cleanest" way for doing that?

Thank you

like image 409
DigitalEngineer Avatar asked Aug 09 '21 21:08

DigitalEngineer


People also ask

Is jetpack compose the future of Android UI?

Jetpack Compose comes with all the functionality needed to build a rich and responsive application UI and features full interoperability with current Android views, making it easy for developers to implement in existing projects.

What is preference screen?

Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences.

How do I show compose preview?

Click the Deploy to Device icon next to the @Preview annotation or at the top of the preview, and Android Studio will deploy that @Preview to your connected device or emulator.

What is scaffold Android jetpack compose?

A Scaffold is a layout which implements the basic material design layout structure. You can add things like a TopBar, BottomBar, FAB or a Drawer.


1 Answers

I have implemented an app in compose and I created the settings screen using the tools available in Jetpack Compose, without fragments.

The way I did this is create a composable for each settings option, that contains a title, and optional subtitle, and a checkbox, which indicates if the option is enabled or not.

These options are then added to a column (or a grid, if you have a tablet, it's easy to support both in compose); you just have to hook the clicks on the checkbox to your viewmodel to change the setting and then refresh the UI.

If you have other kinds of settings entries you can define your own composables for those as well. In my case I have other settings options that open a dialog where the user can configure some parameters, I have another composable function for this kind of settings row as well.

This is personal opinion, but I prefer to stay away from 3rd party libraries unless a) they provide significant value and b) they're from a source that offers some kind of maintenance and bug fixes guarantees. I don't know about that library you mentioned, but if you include 3rd party libraries, you need to be aware you are relinquishing control over parts of your app so you need to balance the cost/benefit ratio.

like image 59
Francesc Avatar answered Oct 22 '22 14:10

Francesc