PreferenceActivity Basics


SDK Version: 
M3
The PreferenceActivity class is a very easy way to create a standard looking settings screen for any application.

Minimal coding is required. The values are stored in SharedPreferences automatically by the PreferenceActivity.

The list of settings, default values, keys to stored under are defined in a very simple xml file.

The xml file should be located in res/xml/ directory and look like something like this:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:title="Settings">
  4.  
  5.         <EditTextPreference android:title="Name"
  6.                 android:inputType="text" android:summary="Set your login name here"
  7.                 android:key="name" android:persistent="true" android:defaultValue=""></EditTextPreference>
  8.                
  9.         <EditTextPreference android:title="Password"
  10.                 android:inputType="textPassword" android:summary=""
  11.                 android:key="password" android:persistent="true" android:defaultValue=""></EditTextPreference>
  12.  
  13.         <CheckBoxPreference android:summary="Auto login on phone boot" android:title="Auto login"
  14.          android:key="autostart" android:defaultValue="true"></CheckBoxPreference>
  15. </PreferenceScreen>

The activity that implements a minimal settings screen only contains one line of code:

  1. public class Settings extends PreferenceActivity{
  2.  
  3.        @Override
  4.        protected void onCreate(Bundle savedInstanceState) {
  5.                super.onCreate(savedInstanceState);
  6.                addPreferencesFromResource(R.xml.nameofpreferencesxmlfile);
  7.                 }
  8. }

You can acces the values of settings anywhere in the application later using DefaultSharedPreferences:

  1. SharedPreferences sp=PreferenceManager.
  2.                 getDefaultSharedPreferences(context);
  3. String value=sp.getString("key","defaultvalue");

There is one tip to keep in mind even if you dont need more than minimal functionality:
In order to avoid defining the default values of Preferences in multiple places, use the

PreferenceManager.setDefaultValues(context, R.xml.nameofpreferencesxmlfile, false) call somewhere early in your

application, for example in the first lines ot the first running activity.
This will set the Preference values to the dafault values defined in the xml file, so anywhere you get values

from preferences you can leave the default value parameter as null or 0 and still get the proper default value.

  1. PreferenceManager.setDefaultValues(context, R.xml.nameofpreferencesxmlfile, false);
  2. SharedPreferences sp=PreferenceManager.
  3.                 getDefaultSharedPreferences(context);
  4. String value=sp.getString("key",null);
  5. //value contains the default value defined in xml instead of null

We will look into some custom functionality using PreferencesActivity in a future tutorial.