Tutorial details

Android beginner tutorial: Part 82 - PreferenceCategory and PreferenceScreen | App Code for Sale | Preview

Android beginner tutorial: Part 82 - PreferenceCategory and PreferenceScreen | Android Tutorial

Grouping preferences in categories and screens

Overview PAGE TOP

In this tutorial we'll learn about grouping preferences in categories and screens.

In some cases you might find that our settings screen is clustered with lots of preference items. You'll want to find a way to group them all nice and tidy, so that the user doesn't have trouble finding the settings he is looking for.

The first way to do so is group preferences of the same topic under PreferenceCategories. By doing this, the grouped preferences will have a title over them, which explains the role of the grouped components.

Example:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory 
        android:title="@string/pref_visual"
        android:key="pref_visual">
    <EditTextPreference
        android:key="pref_textsize"
        android:title="@string/pref_textsize"
        android:dialogTitle="@string/pref_textsize_descr"
        android:summary="@string/pref_textsize_summ"
        android:defaultValue="14" />
    <ListPreference
        android:key="pref_theme"
        android:title="@string/pref_theme"
        android:dialogTitle="@string/pref_theme_descr"
        android:summary="@string/pref_theme_summ"
        android:entries="@array/theme_colors"
        android:entryValues="@array/theme_colors"
        android:defaultValue="Blue" />
    </PreferenceCategory>
    <PreferenceCategory 
        android:title="@string/pref_sound"
        android:key="pref_sound">
    <RingtonePreference
        android:key="pref_tone"
        android:title="@string/pref_tone"
        android:summary="@string/pref_tone_summ"
        android:showDefault="true"
        android:showSilent="true"/>
    <CheckBoxPreference
        android:key="pref_mute"
        android:title="@string/pref_mute"
        android:summary="@string/pref_mute_summ"
        android:defaultValue="false" />
    </PreferenceCategory>
</PreferenceScreen>

You can see that I grouped all the visual-related preferences and all sound-related ones in different categories. Their titles are stored in strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">CodeForFood Test Two</string>
    <string name="pref_mute">Mute</string>
    <string name="pref_mute_summ">Disable all the sound in the application</string>
    <string name="pref_textsize">Text size</string>
    <string name="pref_textsize_descr">Enter text size</string>
    <string name="pref_textsize_summ">Set text size of the interface</string>
    <string name="pref_theme">Theme</string>
    <string name="pref_theme_descr">Pick a color</string>
    <string name="pref_theme_summ">Set the application\'s color theme</string>
    <string name="pref_tone">Ringtone</string>
    <string name="pref_tone_summ">Set ringtone</string>
    <string name="pref_visual">Visual settings</string>
    <string name="pref_sound">Sound settings</string>
    <string name="menu_settings">Settings</string>
    <string name="clear_settings">Reset settings</string>

</resources>

The second way to group preferences is using PreferenceScreen nodes.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceScreen
        android:title="@string/pref_visual"
        android:key="pref_visual">
    <EditTextPreference
        android:key="pref_textsize"
        android:title="@string/pref_textsize"
        android:dialogTitle="@string/pref_textsize_descr"
        android:summary="@string/pref_textsize_summ"
        android:defaultValue="14" />
    <ListPreference
        android:key="pref_theme"
        android:title="@string/pref_theme"
        android:dialogTitle="@string/pref_theme_descr"
        android:summary="@string/pref_theme_summ"
        android:entries="@array/theme_colors"
        android:entryValues="@array/theme_colors"
        android:defaultValue="Blue" />
    </PreferenceScreen>
    <PreferenceScreen 
        android:title="@string/pref_sound"
        android:key="pref_sound">
    <RingtonePreference
        android:key="pref_tone"
        android:title="@string/pref_tone"
        android:summary="@string/pref_tone_summ"
        android:showDefault="true"
        android:showSilent="true"/>
    <CheckBoxPreference
        android:key="pref_mute"
        android:title="@string/pref_mute"
        android:summary="@string/pref_mute_summ"
        android:defaultValue="false" />
    </PreferenceScreen>
</PreferenceScreen>

By including PreferenceScreens inside a PreferenceScreen, the parent displays a clickable item for each PreferenceScreen, which opens that screen with all its contents as a separate Activity.

That's all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/02/android-beginner-tutorial-part-82.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.