Simple Settings Library — Build a Settings Screen in Seconds
Most Android developers would agree, that building a settings screen, even with the Jetpack Preference API, is quite painful. You have to create the xml file, the activity and the settings fragment where the preferences are getting rendered. Especially realizing dynamic preferces for certain purposes is a small challenge and the resulting code is everything, but not clean. This article describes a simplification for this, using the SimpleSettings library. With this library, you can specify your preference configuration directly in your Kotlin/Java file or, if you want to stick with the xml version, be happy: this is possible as well and the recommended way for large and complex preference configurations!
This screenthot shows a preference screen, which was generated with only a few lines of Kotlin code.
SimpleSettings is an extension of the existing framework, provided by the Jetpack Preference API by Google, which makes it a lot more easier to get started with a settings screen. Futhermore, SimpleSettings gives you useful extensions, e.g. predefined PreferenceClickListeners (WebsiteClickListener, PlayStoreClickListener, etc.), which can easily be attached to preference items (More about this later on).
Also, there are lots of useful additional features. For instance, you can show menu items dynamically in the toolbar or show a reset option for resetting all preferences at once. These options can be directly passed to the SimpleSettings instance, which will be demonstrated later in this article.
Setup your settings screen
As always, the first step is to add the dependencies of the library. To do so, please add the repository to your build.gradle file on project level:
And add the following dependencies to your build.gradle file at module level (e.g. app/build.gradle):
Then you have two options to choose: Kotlin or XML.
Let’s assume you’re developing a flight booking app and you want to provide a SwitchPreference item for each booked flight on the central settings screen, where the user can enable/disable status notifications about this specific flight. With the standard Jetpack Preference API, this behaviour would be hard to achieve, whereas SimpleSettings comes with a simple solution for this problem. The settings screen can be populated with nested Kotlin objects like this:
The library uses the same concept, which Jetpack Compose uses, but with even less boilerplate, due to the special use cases.
If you only have a few preference items to handle, this is also the recommended way to setup your settings screen.
The second option is to provide a xml file.
To generate an activity from this XML configuration, you can tie this file to the SimpleSettings builder instance with an one-liner:
Customize the SettingsActivity
There are a few customization options for the underlying activity:
- Providing menu items for the toolbar
- Enabling a reset option for the user to reset all preferences
- Disabling “HomeAsUpEnabled” for the activity
- More to come …
You can apply these options by creating a SimpleSettingsConfig instance and pass it to the SimpleSettings construcor. The example below also shows the usage of a PreferenceClick callback for custom ClickListeners. One of them is set to the above mentioned WebsiteClickListener. You just have to pass the URL and the library does the rest for you. Fairly simple isn’t it?
DialogClickListener for a Kotlin configuration:
WebsiteClickListener for a XML configuration:
Migrating Jetpack Preference to SimpleSettings
The migration from Jetpack Preference to the SimpleSettings lib is quite simple. You can use the same XML preference file, which you have used with the Jetpack Preference framework to setup a SimpleSettings screen. Just delete your SettingsActivity and the SettingsFragment and you are ready to go! Just use the code snippet above to initialize the library with a XML configuration.