The Preference library allows you to build interactive settings screens, without needing to handle interacting with device storage or managing the user interface.
Settings
Settings allow users to change the functionality and behavior of an application. Settings can affect background behavior, such as how often the application synchronizes data with the cloud, or they can be more wide-reaching, such as changing the contents and presentation of the user interface.
The recommended way to integrate user configurable settings into your application is to use the AndroidX Preference Library. This library manages the user interface and interacts with storage so that you define only the individual settings that the user can configure. The library comes with a Material theme that provides a consistent user experience across devices and OS versions.
How to create a Settings Screen
You can create a Settings screen in two way: via xml or via code. Here is the screen that we will build in the two ways:
(a). Via XML
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:key="notifications"
app:title="Enable message notifications"/>
<Preference
app:key="feedback"
app:title="Send feedback"
app:summary="Report technical issues or suggest new features"/>
</PreferenceScreen>
The above xml will give you the following:
(b). Via Code
You can also build a settings screen programmatically via code. Below are code examples:
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val context = preferenceManager.context
val screen = preferenceManager.createPreferenceScreen(context)
val notificationPreference = SwitchPreferenceCompat(context)
notificationPreference.key = "notifications"
notificationPreference.title = "Enable message notifications"
val feedbackPreference = Preference(context)
feedbackPreference.key = "feedback"
feedbackPreference.title = "Send feedback"
feedbackPreference.summary = "Report technical issues or suggest new features"
screen.addPreference(notificationPreference)
screen.addPreference(feedbackPreference)
preferenceScreen = screen
}
Adding a [PreferenceCategory
is similar, as shown below. Remember to add the children to the PreferenceCategory
and not to the root PreferenceScreen
:
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
val context = preferenceManager.context
val screen = preferenceManager.createPreferenceScreen(context)
val notificationPreference = SwitchPreferenceCompat(context)
notificationPreference.key = "notifications"
notificationPreference.title = "Enable message notifications"
val notificationCategory = PreferenceCategory(context)
notificationCategory.key = "notifications_category"
notificationCategory.title = "Notifications"
screen.addPreference(notificationCategory)
notificationCategory.addPreference(notificationPreference)
val feedbackPreference = Preference(context)
feedbackPreference.key = "feedback"
feedbackPreference.title = "Send feedback"
feedbackPreference.summary = "Report technical issues or suggest new features"
val helpCategory = PreferenceCategory(context)
helpCategory.key = "help"
helpCategory.title = "Help"
screen.addPreference(helpCategory)
helpCategory.addPreference(feedbackPreference)
preferenceScreen = screen
}
More Examples
Here are more examples
1. Kotlin Android PreferenceX Example
Let us look at a Android PreferenceXExample-android-master example.
This example will comprise the following files:
MainActivity.kt
SettingsFragment.kt
SettingsSyncFragment.kt
Step 1: Create Project
- Open your
AndroidStudio
IDE. - Go to
File-->New-->Project
to create a new project.
Step 2: Add Dependencies
In your app/build.gradle
add dependencies as shown below. Include the androidx.preference:preference
as a dependency:
implementation "androidx.preference:preference:1.1.0-alpha01"
Step 3: Design Layouts
*(a). activity_main.xml
Create a file named activity_main.xml
and design it as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
*(b). content_main.xml
Create a file named content_main.xml
and design it as follows:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Write Code
Write Code as follows:
(a). MainActivity.kt
Create a file named MainActivity.kt
and implement the PreferenceFragmentCompat.OnPreferenceStartFragmentCallback
interface then override the onPreferenceStartFragment
function:
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat?, pref: Preference?): Boolean = false
Here is the full code
package com.numero.preference_androidx
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
supportFragmentManager.beginTransaction()
.replace(R.id.container, SettingsFragment())
.commit()
}
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat?, pref: Preference?): Boolean = false
}
(b). SettingsFragment.kt
Create a file named SettingsFragment.kt
and extend the PreferenceFragmentCompat
then override the onCreatePreferences
as shown below:
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
}
Here is the full code
package com.numero.preference_androidx
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings, rootKey)
}
}
(c). SettingsSyncFragment.kt
Create a file named SettingsSyncFragment.kt
and extend the PreferenceFragmentCompat
then override the onCreatePreferences
and onCreate
functions as shown:
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings_sync, rootKey)
}
Here is the full code
package com.numero.preference_androidx
import android.os.Bundle
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
class SettingsSyncFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.settings_sync, rootKey)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val syncSummaryProvider = Preference.SummaryProvider<SwitchPreferenceCompat> { preference ->
if (preference.isChecked) {
"Checked"
} else {
"Non checked"
}
}
findPreference("enable_sync").summaryProvider = syncSummaryProvider
}
}
Run
Simply copy the source code into your Android Project,Build and Run.