Understanding Android's Preference Framework

One of the great things about the Android preference framework is how simple it is to develop a screen that allows the user to modify preferences. Most of the heavy lifting is done for you by Android, because developing a preference screen is as simple as defining a preference screen in XML that is located in the res/xml folder of your project. Although these XML files are not the same as layout files, they are specific XML definitions that define screens, categories, and actual preferences. Common preferences that are built into the framework include the following:

  • EditTextPreference: A preference that can store plain text as a string
  • CheckBoxPreference: A preference that can store a boolean value
  • ListPreference: A preference that allows the user to select a preferred item from a list of items in the dialog box

If the built-in preferences do not suit your needs, you can create your own preference by deriving from the base Preference class or from DialogPreference the base class for dialog box–based preferences. When clicked, these preferences open a dialog box showing the actual preference controls. Examples of built-in DialogPreferences are EditTextPreference and ListPreference.

Android also provides a PreferenceActivity from which you can derive and load the preference screens in the same manner that you would load a layout for a basic Activity class. The PreferenceActivity base class allows you to tap into the PreferenceActivity ...

Get Android™ Tablet Application Development For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.