Friday, 20 September 2013

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.

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 attributeandroid: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”).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

<string-arrayname="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-arrayname="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.

Advance Preference

Java Code

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

1

2

3

4

5

6

7

8

publicclassPreferenceExample extendsPreferenceActivity{

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preference);

}

}

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.

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

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.