How to Implement Preferences Settings Screen in Android?
Last Updated :
24 Dec, 2022
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.
What we are going to build in this article?
We will be building a simple application in which we will be displaying a simple button and on clicking on that button we will be opening a settings screen which we will create using settings preferences. This settings screen will look similar to what we can get to see in YouTube settings in the General option. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".MainActivity">
<!--button for opening settings activity-->
<Button
android:id="@+id/idBtnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Settings"
android:textAllCaps="false" />
</RelativeLayout>
Step 3: Creating a new activity for displaying the settings screen
Navigate to the app > java > your app's package name > Right-click on it > New > Activity and select Empty activity and name it as SettingsActivity.
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// creating a variable for our button.
private Button settingsBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our button.
settingsBtn = findViewById(R.id.idBtnSettings);
// adding on click listener for our button.
settingsBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// opening a new intent to open settings activity.
Intent i = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(i);
}
});
}
}
Step 5: Creating a preference file for displaying our settings
Navigate to the app > res > Right-click on it > New > Android Resource file and you will get to see the below screen.

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.
XML
<resources>
<string name="app_name">GFG App</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: Working with preferences.xml file
In preferences there are different types of preferences which are listed below :
- EditTextPreference: this is used to get the text from the user.
- ListPreference: this option is used to display a dialog with the list of options to choose from.
- CheckBoxPreference: this option is used to display a checkbox to toggle a setting.
- SwitchPreference: this option is used to turn the switch on and off.
- RingtonePreference: this option is used to open the ringtone page of your device.
- Preference with an Intent action android.intent.action.VIEW – to open an external browser navigating to an URL.
Navigate to the app > res > xml > preferences.xml file and add the below code to it. Comments are added in the code to get to know in more detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<!--below line is to create preference category-->
<PreferenceCategory android: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" />
<!--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-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/remind_me_for_bed_time"
android:title="@string/remind_for_bed_time" />
<!--below switch preference is
use for mobile data usage-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/limit_data_usage"
android:summary="@string/stream_video"
android:title="@string/limit_mobile_usage" />
<!--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" />
<!--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" />
<!--below switch preferences is use to restrict mode-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/prefs_restricted_mode"
android:summary="@string/restricted_mode"
android:title="@string/restricated_mode_description" />
<!--below switch pref is use for enable stats option-->
<SwitchPreference
android:defaultValue="false"
android:key="@string/prefs_enable_stats"
android:title="@string/enable_stats" />
</PreferenceCategory>
</PreferenceScreen>
Step 8: Now create a new java class for displaying our preference fragment
Navigate to the app > java > your app's package name > Right-click on it > New > Java 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.
Java
import android.os.Bundle;
import android.preference.PreferenceFragment;
import androidx.annotation.Nullable;
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// below line is used to add preference
// fragment from our xml folder.
addPreferencesFromResource(R.xml.preferences);
}
}
Step 9: Working with activity_settings.xml file
Navigate to the activity_settings.xml file and add the below code to it. Comments are added in the code to get to know in more detail.
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 10: Working with SettingsActivity.java file
Navigate to the app > java > your app's package name > SettingsActivity.java file and add the below code to it. Comments are added in the code to get to know in more detail.
Java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// below line is to change
// the title of our action bar.
getSupportActionBar().setTitle("Settings");
// below line is used to check if
// frame layout is empty or not.
if (findViewById(R.id.idFrameLayout) != null) {
if (savedInstanceState != null) {
return;
}
// below line is to inflate our fragment.
getFragmentManager().beginTransaction().add(R.id.idFrameLayout, new SettingsFragment()).commit();
}
}
}
Now run your app and see the output of the app.
Output:
Similar Reads
Android - Implement Preference Setting Screen with Kotlin
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
5 min read
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
How to Prevent Screenshot Or Screen Recorder in Android?
In some situations, we don't want to allow to take screenshots or screen recordings of our android application. Here we are going to explain how to prevent Android from taking a screenshot or screen recording when the app goes to the background. Generally, when we take a screenshot, we will see a Sc
3 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
How to Implement OnSavedInstanceState in Android?
In android, Preserving and restoring an activity's UI state in a timely fashion across system-initiated activity or application destruction is a crucial part of the user experience. In these cases the user expects the UI state to remain the same, but the system destroys the activity and any state st
4 min read
How to implement Picture in Picture (PIP) in Android?
In this article, it is explained how to implement a Picture in Picture (PIP) in an Android application. We have seen in many apps such as Google Maps while using navigation that when we close the app, there is a floating screen that appears at the bottom right of the screen as shown in the image bel
3 min read
How to Implement Date Range Picker in Android?
Date Range Picker is a widely used feature in many popular Android apps and an essential component of Material Design. It allows users to select a range of dates such as a start and end date for various purposes including scheduling, filtering data, and setting time boundaries. Some Of The Real Life
4 min read
How to View Data Stored in Shared Preferences in Android Studio?
Shared Preferences in Android is local storage where we can save our data using a key and value pair. It is generally used to store data in users' devices. Shared Preferences is also used for session management in Android apps where we are using login functionality. In this article, we will take a l
4 min read
How to Implement TNImage Library in Android?
Android is an open-source operating system, based on the Linux kernel and used in mobile devices like smartphones, tablets, etc. Further, it was developed for smartwatches and Android TV. Each of them has a specialized interface. Android has been one of the best-selling OS for smartphones. Android O
3 min read
How to Implement Custom Searchable Spinner in Android?
Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it. The default value of the android spinner will be the c
5 min read