Android - Implement Preference Setting Screen with Kotlin
Last Updated :
21 Apr, 2025
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at implementing the preferences setting screen in Android using Kotlin.
Note: If you are looking to implement a Preference Setting Screen in Android using Java. Check out the following article: How to Implement Preference Setting Screen in Android using Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Note that select Kotlin as the programming language.
Step 2: Add dependency for Preference Library
Navigate to Gradle Scripts > build.gradle.kts (Module :app) and add the following dependency
dependencies {
...
implementation ("androidx.preference:preference:1.2.1")
}
Step 3: Working with MainActivity.kt
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail. Then, Navigate to the app > res > layout > activity_main.xml and add the below code to that file.
MainActivity.kt
package org.geeksforgeeks.demo
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var settingsBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
settingsBtn = findViewById(R.id.idBtnSettings)
settingsBtn.setOnClickListener {
startActivity(Intent(this, SettingsActivity::class.java))
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<!--button for opening settings activity-->
<Button
android:id="@+id/idBtnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Settings"
android:textAllCaps="false" />
</LinearLayout>
Step 4: Creating a new activity for displaying the settings screen
Navigate to the app > java+kotlin > {package-name}, Right-click on it, New > Activity and select Empty activity and name it as SettingsActivity.
SettingsActivity.kt
package org.geeksforgeeks.demo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
supportActionBar?.title = "Settings"
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.idFrameLayout, SettingsFragment())
.commit()
}
}
}
activity_settings.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsActivity">
<!--frame layout for displaying
our preference fragment-->
<FrameLayout
android:id="@+id/idFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Creating a preference file for displaying our settings
Navigate to the app > res > xml, Right-click on it, New > Android Resource file and name the file as preferences.xml.
preferences.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--below line is to create preference category-->
<PreferenceCategory
app:icon="@android:drawable/ic_menu_manage"
app:title="General">
<!--in below line we are creating a list preference
and we are adding default selected value in list for 3 rd index-->
<!--dialog title is to set title for our dialog box
entries is used to add list of data which we
are adding from our strings file
entry values is to add values to our entries.
key is use to add key to our list preferences
summary is use to add description to our option
title is use to add title to our list preferences.-->
<!--this list preference is for remind me option-->
<ListPreference
android:defaultValue="3"
android:dialogTitle="@string/remind_to_take_a_break"
android:entries="@array/pref_remind_me_to_take_a_break"
android:entryValues="@array/pref_duration"
android:key="@string/key_upload_quality"
android:summary="@string/remind_me"
android:title="@string/remind_me"
app:icon="@android:drawable/ic_menu_day" />
<!--on below line we are creating a switch preference
default value is use to set switch on or off
key is use to set key
title is use to add title to our switch-->
<!--this switch preference option is to remind for a bed time-->
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/remind_me_for_bed_time"
android:title="@string/remind_for_bed_time"
app:icon="@android:drawable/btn_star" />
<!--below switch preference is
use for mobile data usage-->
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/limit_data_usage"
android:summary="@string/stream_video"
android:title="@string/limit_mobile_usage"
app:icon="@android:drawable/ic_menu_call" />
<!--below list preference is use for
double tap to seek option-->
<ListPreference
android:defaultValue="1"
android:dialogTitle="@string/double_tap_to_seek"
android:entries="@array/pref_seek_values"
android:entryValues="@array/pref_duration"
android:key="@string/pref_seek_val"
android:summary="@string/seconds"
android:title="@string/double_tap_to_seek"
app:icon="@android:drawable/ic_menu_help" />
<!--below option is use to create a list
preference for Upload preferences-->
<ListPreference
android:defaultValue="1"
android:dialogTitle="@string/uploads"
android:entries="@array/pref_uploads"
android:entryValues="@array/pref_duration"
android:key="@string/pref_uploads"
android:summary="@string/specify_network_prefs"
android:title="Uploads"
app:icon="@android:drawable/ic_menu_save" />
<!--below switch preferences is use to restrict mode-->
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/prefs_restricted_mode"
android:summary="@string/restricted_mode"
android:title="@string/restricted_mode_description"
app:icon="@android:drawable/ic_menu_compass" />
<!--below switch pref is use for enable stats option-->
<SwitchPreferenceCompat
android:defaultValue="false"
android:key="@string/prefs_enable_stats"
android:title="@string/enable_stats"
app:icon="@android:drawable/ic_menu_info_details" />
</PreferenceCategory>
</PreferenceScreen>
Step 6: Add the below code in the strings.xml file
Navigate to the app > res > values > strings.xml file and add the below code to it.
strings.xml:
XML
<resources>
<!--app name-->
<string name="app_name">Preference Setting Screen</string>
<string name="remind_to_take_a_break">Remind me to take a break</string>
<string name="key_upload_quality">key_upload_quality</string>
<string name="remind_me">Remind me to take a break</string>
<string name="remind_me_for_bed_time">remind_me_for_bed_time</string>
<string name="remind_for_bed_time">Remind me for bed time</string>
<string name="limit_data_usage">prefs_limit_data_usage</string>
<string name="stream_video">Only stream HD video on Wi-Fi</string>
<string name="limit_mobile_usage">Limit mobile data usage</string>
<string name="double_tap_to_seek">Double-tap to seek</string>
<string name="pref_seek_val">pref_seek_val</string>
<string name="seconds">seconds</string>
<string name="uploads">Uploads</string>
<string name="pref_uploads">pref_uploads</string>
<string name="specify_network_prefs">Specify network preferences for uploads</string>
<string name="prefs_restricted_mode">prefs_restricted_mode</string>
<string name="restricted_mode">Restricted Mode can help to hide videos with potentially mature content.No filter is 100% accurate, but it should help you to avoid most of this type of content.</string>
<string name="restricted_mode_description">Restricted Mode</string>
<string name="prefs_enable_stats">prefs_enable_stats</string>
<string name="enable_stats">Enable stats for nerds</string>
<string-array name="pref_upload_quality_entries">
<item>360p</item>
<item>480p</item>
<item>720p</item>
<item>1080p</item>
<item>Original</item>
</string-array>
<string-array name="pref_remind_me_to_take_a_break">
<item>1 hours</item>
<item>2 hours</item>
<item>3 hours</item>
<item>5 hours</item>
<item>10 hours</item>
</string-array>
<string-array name="pref_seek_values">
<item>5</item>
<item>10</item>
<item>15</item>
<item>20</item>
<item>30</item>
</string-array>
<string-array name="pref_duration">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
<string-array name="pref_uploads">
<item>Only when on Wi-Fi</item>
<item>On any network</item>
</string-array>
</resources>
Step 7: Now create a new Kotlin class for displaying our preference fragment
Navigate to the app > java+kotlin > your app’s package name, Right-click on it, New > Kotlin class and name it as SettingsFragment and add the below code to it. Comments are added in the code to get to know in more detail.
Kotlin
package org.geeksforgeeks.demo
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
Output:
Similar Reads
How to Implement Preferences Settings Screen in Android?
In many apps, we have seen the Settings screen which is most common in most of the apps. This settings screen is used to manage the preferences of the users. For creating this settings screen android provides a feature to make a settings preferences screen. In this article, we will take a look at im
7 min read
Shared Preferences in Android with Example
One of the most Interesting Data Storage options Android provides its users is Shared Preferences. Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up you
7 min read
Android - Login and Logout Using Shared Preferences in Kotlin
Using Shared Preferences We can store data locally on our Android applications. In your Android app if you are using a feature of Login then you should have to save the state if the user has signed the first time. Then when the user closes his app and reopens it then he should redirect to our Home s
6 min read
Android Jetpack Compose - Open Specific Settings Screen
Many times while the user is using an android application he has to edit a few settings in the device to use the applications such as providing any permissions from the settings application to use that specific feature within the application. For changing these settings we have to open a specific se
3 min read
Android - Save ArrayList to SharedPreferences with Kotlin
SharedPreferences is local storage in android which is used to store data in the form of key and value pairs within the android devices. We can store data in the form of key and value pairs using Shared Preferences. In this article, we will take a look at How to Save Array List to SharedPreferences
9 min read
Android - SearchView with RecyclerView using Kotlin
Many apps display vast amounts of data within their in the form of a list and the user is not able to go through each item within the list to get the item that he wants. For filtering these lists within the android application we can use SearchView to filter it. In this article, we will be building
7 min read
Android Session Management with Kotlin
Session Management is seen in most of the android applications in which we can get to know the login and sign-up page. This is used to store the session of the user when he is logged into the application. When the user login inside the application for the first time. The user credentials such as ema
7 min read
How to Delete Shared Preferences Data in Android?
In Android, Shared Preferences is a method of storing and fetching primitive data with the help of a key. This is file is stored inside the application in the form of an XML file. It is used to save data of types int, long, string, boolean, etc. In this article, we will show you how you could save a
3 min read
Encrypted Shared Preferences in Android
Many times we want to save the data within the android application. This data is in the form of text and we generally prefer to store this data in the shared preferences. Shared preferences are not secure as we can simply view the data stored within the shared preferences and can easily access data
5 min read
Multiple Runtime Permissions in Android with Kotlin using Dexter
From MarshMallow android introduced runtime permissions, we can grant the permissions while using the app, instead of while installing the app. Â In general, If you want to use a camera or make a call from your app you will ask for the user's permission. There are some predefined functions provided t
3 min read