# Android Preferences

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, RingtonePreference and CheckBoxPreference via sample code.

Below screen shot is the output showing all the standard preferences.

Android Preferences

As show above preferences are classified in to 3 categories.

• Main: CheckBox Preference
• Other Preferences: List Preference, EditText Preference and Ringtone Preference
<?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>
<PreferenceCategory android:title="Other Prefernces">
<ListPreference android:title="List Preference"
android:key="DayOfWeek"
android:dependency="EnablePreferences"
android:summary="Selec 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:defaultValue="Android Partaker"/>
<RingtonePreference android:title="Ringtone Preference"
android:key="Ringtone"
android:dependency="EnablePreferences"
android:summary="Select Ringtone"
android:ringtoneType="all" />
</PreferenceCategory>

<EditTextPreference android:title="Enter Text"
android:key="Text" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>


Let us look at the preference.xml (res/xml) for the above 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. As seen there are 3 PreferenceCategory Main, Other Preferences and Advance Preferenc.

CheckBoxPreference defines preference of type CheckBox which can either be checked or unchecked. Here in the example key EnablePreferences defined via attribute android:key will hold the value whether CheckBox is checked or not.CheckBoxPreference is defined under PreferenceCategory Main (android:title)

CheckBox Preference

PreferenceCategory Other Preferences group ListPreference, EditTextPreference and RingtonePreference. Before we go ahead and understand them let me point one attribute which is in common to all the three preference this category.

All the 3 preferences have attribute android:dependency in common. Has the name suggest this attribute android:dependency set a dependency. As seen here in the example the value for this attribute is EnablePreferences which actually is the key of CheckBoxPreference of PreferenceCategory Main. Thus the dependency of all the 3 preferences in Other Preferences category is with CheckBoxPreference i.e. this preferences will be enabled only if CheckBox Preference is checked.

CheckBox Preference

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 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"?>
<resources>
<string-array name="daysOfWeek">
<item>Sunday</item>
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
</string-array>
<string-array name="daysOfWeekValues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</string-array>
</resources>


List Preference

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.

EditText Preference

RingtonePreference will show the list of ringtones and also allow selecting default ringtone or selecting silent mode. One can control the list of ringtone to be shown via attribute android:ringtoneType. Valid values for ringtone type are ringtone, notification, alarm and all.

Ringtone Preference

PreferenceCategory Advance Preference defines another PreferenceScreen Advance Preference. PreferenceScreen is configured with EditTextPreference. Hence this new PreferenceScreen opens with with EditTextPreference as configured.

Java Code

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

public class PreferenceExample extends PreferenceActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}


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

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.

Initialize Preferences with default value

When we 1st use Preferences they are empty, but in many cases we need to initialize preferences with some default values. We can achieve this with 1st setting default value using attribute android:defaultValue in xml and then use following line of code to initialize preferences with the set default values

PreferenceManager.setDefaultValues(PreferenceExample.this, R.xml.preference, false);

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 PreferenceExample extends PreferenceActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);