Most of applications have some setting which user can/needs to configure, and to do this setting a screen needs to be designed along with code to read the data and display and update the data back.

In Android we can do this with minimum efforts via Preferences. Another advantage of using Android preference for application setting helps Android user because it will be uniform across the applications.

In this article we will look at the standard Preference available i.e. ListPrefernce, EditTextPreference and CheckBoxPreference via sample code.
Let’s create the project.

1. Create Project

Select File -> New -> Other -> Android -> Android Project and create the Android project “com.mobisys.android.demo1_preferences”. Enter the following.

2. Create Preferences

Create an Android XML resource “preferences.xml” of type “PreferenceScreen”.

After Finish, Click on preferences.xml tab & write the following code in it.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
	<PreferenceCategory android:title="Main">
		<CheckBoxPreference android:title="Enable Preferences"
			android:key="EnablePreferences" android:summary="Check to enable Other Preferences" />
	<PreferenceCategory android:title="Other Prefernces">
		<ListPreference android:title="List Preference"
			android:key="DayOfWeek" android:dependency="EnablePreferences"
			android:summary="Select Day of the Week" android:entries="@array/daysOfWeek"
			android:entryValues="@array/daysOfWeekValues" />
		<EditTextPreference android:title="Edit Text Preference"
			android:key="Name" android:dependency="EnablePreferences"
			android:summary="Enter Your Name" android:dialogTitle="Enter Your Name"
			android:defaultValue="vikas hiran"/>

Let us look at the above preferences.xml (res/xml) for the example to understand each of the preferences.

PreferenceScreen is the root element of the xml and is

use to define screen for Preference.

PreferenceCategory element is use for grouping the preference.

CheckBoxPreference defines preference of type CheckBox which can either be checked or unchecked. Here in the example key EnablePreferences defined. CheckBoxPreference is defined under PreferenceCategory.

PreferenceCategory Other Preferences group into ListPreference, EditTextPreference.
These 2 preferences have attribute android:dependency in common. Has the name suggest this attribute android:dependency set a dependency.
these 2 preferences will be enabled only if CheckBox Preference is checked. i.e

ListPreference this preference display list of values and help is selecting one. In the above example ListPreference will show list of Days of Week. List of days of week is provided via xml file daysOfWeek.xml (res/values) as shown below.

Attribute android:entries (defined in preferences.xml) will point to arrays daysOfWeek (android:entries=“@array/daysOfWeek”) and android:entryValue hold the corresponding value defined for each of the days of the week (android:entryValues=“@array/daysOfWeekValues”).

<?xml version="1.0" encoding="utf-8"?>
	<string-array name="daysOfWeek">
	<string-array name="daysOfWeekValues">


EditTextPreference as name suggest all editing of the value it holds. We can set title of the dialog which is opened on click via android:dialogTitle in case this attribute is not set then Preference title (android:title) is used as dialog title.

3. Java Code

Below is the code snippet for PreferenceActivity showing how to use the preferences.xml (res/xml) for bring up Preferences.

public class MyPreferences extends PreferenceActivity{

    public void onCreate(Bundle savedInstanceState) {

addPreferencesFromResorce() method will read the preferences.xml from res/xml and will render the PreferenceScreen accordingly.

4. Fetch Values from Preferences

So far we have looked at configuring and bring up the preferences. Now let us look at the code to fetch the values.

SharedPreferences _sharedPreferences  = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String name = _sharedPreferences.getString(“Name”, “NA”);

1. Create instance of SharedPreferences.
2. Now we can use instance of SharedPreference for fetching values. First parameter of getString method is the key of the preference and second parameter represents the value to be returned in case the key is not present.

5. Initialize Preferences with default value

As seen we have set default value for EditTextPreference of Other Preferences category in preference.xml. But when we ran the code without setting the default values in PreferenceActivity we can see empty EditText coming up when we click on EditTextPreference in Other Preferences category. Now modify the code of PreferenceActivity as follows and run to see EditText been initialized with default value.

 public class myPreferences extends PreferenceActivity {
	public void onCreate(Bundle savedInstanceState) {
	    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

6. Download Source Code

Click here to download the source code

AUTHOR: Vikas Hiran
No Comments

Leave a Comment

Your email address will not be published.