Organizing Preferences

The preferences framework provides some support for you to organize your preferences into categories. If you have a lot of preferences, for example, you can build a view that shows highlevel categories of preferences. Users could then drill down into each category to view and manage preferences specific to that group.

You can implement something like this in one of two ways. You can introduce nested PreferenceScreen elements within the root PreferenceScreen, or you can use PreferenceCategory elements to get a similar result. Figure 11-6 and Listing 11-7 show how to implement the first technique: grouping preferences by using nested PreferenceScreen elements.

lede □ sud« 10:35 pml


Preferences related to Meats

Preferences related to vegetable


h ^bd« 10:35 pm i


Fish Is great for the healthy


A common type of poultry

Lamb Isayoung sheep


It's actually a fruit


My favorite vegetable

Figure 11-6. Creating groups of preferences by nesting PreferenceScreen elements

The view on the left of Figure 11-6 displays two preference screens, one with the title "Meats" and the other with the title "Vegetables." Clicking a group takes you to the preferences within that group. Listing 11-7 shows how to create nested screens.

Listing 11-7. Nesting PreferenceScreen Elements to Organize Preferences

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="" android:key="using_categories_in_root_screen" android:title="Categories" android:summary="Using Preference Categories'^

<PreferenceScreen xmlns:android="" android:key="meats_screen" android:title="Meats"

android:summary="Preferences related to Meats">

<CheckBoxPreference android:key="fish_selection_pref" android:title="Fish"

android:summary="Fish is great for the healthy" /> <CheckBoxPreference android:key="chicken_selection_pref" android:title="Chicken"

android:summary="A common type of poultry" /> <CheckBoxPreference android:key="lamb_selection_pref" android:title="Lamb"

android:summary="Lamb is a young sheep" />

</PreferenceScreen> <PreferenceScreen xmlns:android="" android:key="vegi_screen" android:title="Vegetables"

android:summary="Preferences related to vegetable"> <CheckBoxPreference android:key="tomato_selection_pref" android:title="Tomato " android:summary="It's actually a fruit" /> <CheckBoxPreference android:key="potato_selection_pref" android:title="Potato" android:summary="My favorite vegetable" />



You create the groups in Figure 11-6 by nesting PreferenceScreen elements within the root PreferenceScreen. Organizing preferences this way is useful if you have a lot of preferences and you're concerned about having the user scroll to find the preference he is looking for. If you don't have a lot of preferences but still want to provide high-level categories for your preferences, you can use PreferenceCategory, which is the second technique we mentioned. Figure 11-7 and Listing 11-8 show the details.


0 0

Post a comment