including categories and screens.
Categories are added via a PreferenceCategory
element in your preference XML and are used to group together related preferences. Rather than have your preferences all as children of the root PreferenceScreen
, you can put a few PreferenceCategory
elements in the PreferenceScreen
, and then put your preferences in their appropriate categories. Visually, this adds a divider with the category title between groups of preferences.
If you have lots and lots of preferences — more than is convenient for users to scroll through — you can also put them on separate “screens” by introducing the PreferenceScreen
element.
Yes, PreferenceScreen
element.
Any children of PreferenceScreen
go on their own screen. If you nest PreferenceScreens
, the parent screen displays the screen as a placeholder entry — tapping that entry brings up the child screen. For example, from the Prefs/Structured
sample project on the Apress Web site, here is a preference XML file that contains both PreferenceCategory
and nested PreferenceScreen
elements:
<PreferenceScreen
xmlns:android='http://schemas.android.com/apk/res/android'>
<PreferenceCategory android:title='Simple Preferences'>
<CheckBoxPreference
android:key='@string/checkbox'
android:title='Checkbox Preference'
android:summary='Check it on, check it off'
/>
<RingtonePreference
android:key='@string/ringtone'
android:title='Ringtone Preference'
android:showDefault='true'
android:showSilent='true'
android:summary='Pick a tone, any tone'
/>
</PreferenceCategory>
<PreferenceCategory android:title='Detail Screens'>
<PreferenceScreen
android:key='detail'
android:title='Detail Screen'
android:summary='Additional preferences held in another page'>
<CheckBoxPreference
android:key='@string/checkbox2'
android:title='Another Checkbox'
android:summary='On. Off. It really doesn't matter.'
/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
The result, when you use this preference XML with your PreferenceActivity
implementation, is a categorized list of elements like those in Figure 17-4.

Figure 17-4.
And if you tap on the Detail Screen entry, you are taken to the child preference screen (Figure 17-5).

Figure 17-5.
The Kind of Pop-Ups You Like
Of course, not all preferences are checkboxes and ringtones.
For others, like entry fields and lists, Android uses pop-up dialogs. Users do not enter their preference directly in the preference UI activity, but rather tap on a preference, fill in a value, and click OK to commit the change.
Structurally, in the preference XML, fields and lists are not significantly different from other preference types, as seen in this preference XML from the Prefs/Dialogs
sample project available at http://apress.com:
<PreferenceScreen
xmlns:android='http://schemas.android.com/apk/res/android'>
<PreferenceCategory android:title='Simple Preferences'>
<CheckBoxPreference
android:key='@string/checkbox'
android:title='Checkbox Preference'
android:summary='Check it on, check it off'
/>
<RingtonePreference
android:key='@string/ringtone'
android:title='Ringtone Preference'
android:showDefault='true'
android:showSilent='true'
android:summary='Pick a tone, any tone'
/>
</PreferenceCategory>
<PreferenceCategory android:title='Detail Screens'>
<PreferenceScreen
android:key='detail'
android:title='Detail Screen'
android:summary='Additional preferences held in another page'>
<CheckBoxPreference
android:key='@string/checkbox2'
android:title='Another Checkbox'
android:summary='On. Off. It really doesn't matter.'
/>
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory android:title='Simple Preferences'>
<EditTextPreference
android:key='@string/text'
android:title='Text Entry Dialog'
android:summary='Click to pop up a field for entry'
android:dialogTitle='Enter something useful'
/>