Introducing The Preference Framework And The Preference Activity

Android offers an XML-driven framework to create system-style Preference Screens for your applications. By using this framework you can create Preference Activities that are consistent with those used in both native and other third-party applications

This has two distinct advantages:

  • Users will be familiar with the layout and use of your settings screens
  • You can integrate settings screens from other applications (including system settings such as location settings) into your application’s preferences.

The preference framework consists of four parts:

Preference Screen layout — An XML file that defines the hierarchy of items displayed in your Preference screens. It specifies the text and associated controls to display, the allowed values, and the Shared Preference keys to use for each control.

Preference Activity and Preference Fragment — Extensions of PreferenceActivity and PreferenceFragment respectively, that are used to host the Preference Screens. Prior to Android 3.0, Preference Activities hosted the Preference Screen directly; since then, Preference Screens are hosted by Preference Fragments, which, in turn, are hosted by Preference Activities.

Preference Header definition — An XML fi le that defines the Preference Fragments for your application and the hierarchy that should be used to display them

Android API level 11 (Android 3.0) introduced significant changes to the preference framework by introducing the concept of Preference Fragments and
Preference Headers. This is now the preferred technique for creating Activity
Preference screens.
As of the time of writing, Preference Fragments are not included in the support
library, restricting their use to devices Android 3.0 and above.
The following sections describe the best practice techniques for creating Activity
screens for Android 3.0+ devices, making note of how to achieve similar functionality for older devices.

Leave a Comment