Understanding the Android Preferences Framework
One outstanding quality of the Android preferences framework is the simplicity of developing a screen that allows users to modify their preferences. Most of the heavy lifting is done for you by Android because developing a preferences screen is as simple as defining it in the XML located in the res/xml
folder of your project. Though these XML files aren’t the same as layout files, there are specific XML definitions that define screens, categories, and actual preferences. Common preferences that are built into the framework include
EditTextPreference
: Stores plain text as a string
CheckBoxPreference
: Stores a Boolean value
RingtonePreference
: Allows the user to store a preferred ringtone from those available on the device
ListPreference
: Allows the user to select a preferred item from a list of items in the dialog box
If the built-in preferences don’t suit your needs, you can create your own preference by deriving it from the base Preference
class or DialogPreference
. A DialogPreference
is the base class for preferences that are dialog-box-based. Tapping one of these preferences opens a dialog box showing the preference controls. Examples of built-in DialogPreferences
are EditTextPreference
and ListPreference
.
Android also provides a PreferenceActivity
in which you can load a preferences screen in the same manner as you load a layout for a basic Activity
class. This base class allows you to tap into the PreferenceActivity
events and perform advanced work, such as setting an EditTextPreference
to accept only numbers.