: Beginning Android

The Kind of Pop-Ups You Like

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 athttp://apress.com:

<PreferenceCategory android:title="Simple Preferences">
android:title="Checkbox Preference"
android:summary="Check it on, check it off"
android:title="Ringtone Preference"
android:summary="Pick a tone, any tone"
<PreferenceCategory android:title="Detail Screens">
android:title="Detail Screen"
android:summary="Additional preferences held in another page">
android:title="Another Checkbox"
android:summary="On. Off. It really doesn't matter."
<PreferenceCategory android:title="Simple Preferences">
android:title="Text Entry Dialog"
android:summary="Click to pop up a field for entry"
android:dialogTitle="Enter something useful"
android:title="Selection Dialog"
android:summary="Click to pop up a list to choose from"
android:dialogTitle="Choose a Pennsylvania city" />

With the field (EditTextPreference), in addition to the title and summary you put on the preference itself, you can also supply the title to use for the dialog.

With the list (ListPreference), you supply both a dialog title and two string-array resources: one for the display names, one for the values. These need to be in the same order the index of the chosen display name determines which value is stored as the preference in the SharedPreferences. For example, here are the arrays for use by the ListPreference shown previously:

<?xml version="1.0" encoding="utf-8"?>
<string-array name="cities">
<string-array name="airport_codes">

When you bring up the preference UI, you start with another category with another pair of preference entries (see Figure 17-6).

Figure 17-6.The preference screen of the Dialogs projects preference UI

Tapping the Text Entry Dialog preference brings up a text-entry dialog in this case, with the prior preference entry prefilled in (Figure 17-7).

Figure 17-7.Editing a text preference

Tapping the Selection Dialog preference brings up a selection dialog, showing the display names (Figure 17-8).

Figure 17-8.Editing a list preference

: 0.325. /Cache: 3 / 1