Example of Grouping Android Preferences By Title

0

In this example, we will group the settings by title. This will be a continuation of our preferences example.

When you have provided a bunch of settings, it is beneficial if you can group the settings else user might end up scrolling up and down the settings screen searching for a specific setting.

You can group the settings by title as well as using sub-screens.

Group settings by title

Our preferences file had the following settings.

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
        android:title="@string/settings"
        android:summary="@string/settings_summary">

    <CheckBoxPreference
        android:defaultValue="false"
        android:key="perf_show_delete"
        android:title="@string/pref_show_delete" />

    <EditTextPreference
        android:key="pref_list_limit"
        android:summary="@string/pref_summary_list_size"
        android:title="@string/pref_list_limit" />

    <ListPreference
        android:defaultValue="@string/pref_color_theme_value"
        android:dialogTitle="@string/pref_color_theme"
        android:entries="@array/pref_color_theme_entries"
        android:entryValues="@array/pref_color_theme_values"
        android:key="pref_syncConnectionType"
        android:title="@string/pref_color_theme" />

</PreferenceScreen>

Each group will have its own heading and a divider. We will group the settings into two groups. One would be Admin and the other would be Appearances. All we need to do is place each group of Preference objects inside a PreferenceCategory.

Here is the modified preferences.xml.

preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:summary="@string/settings_summary"
    android:title="@string/settings" >

    <PreferenceCategory
        android:key="pref_admin_settings"
        android:title="@string/pref_group_admin" >
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="perf_show_delete"
            android:title="@string/pref_show_delete" />
    </PreferenceCategory>
    <PreferenceCategory
        android:key="pref_appearance_settings"
        android:title="@string/pref_group_appearance" >
        <EditTextPreference
            android:key="pref_list_limit"
            android:summary="@string/pref_summary_list_size"
            android:title="@string/pref_list_limit" />

        <ListPreference
            android:defaultValue="@string/pref_color_theme_value"
            android:dialogTitle="@string/pref_color_theme"
            android:entries="@array/pref_color_theme_entries"
            android:entryValues="@array/pref_color_theme_values"
            android:key="pref_syncConnectionType"
            android:title="@string/pref_color_theme" />
    </PreferenceCategory>

</PreferenceScreen>

The group titles are added to strings.xml.

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

    <string name="app_name">JavArticles</string>
    <string name="welcome">Preferences Example</string>
    <string name="list_size">List Size</string>
    <string name="settings">Settings</string>
    <string name="settings_summary">Example of Preferences</string>
    <string name="pref_show_delete">Show delete button?</string>
    <string name="pref_list_limit">List Size</string>
    <string name="pref_summary_list_size">Maximun rows to be shown in a list page</string>
    <string name="pref_color_theme">Color theme</string>
    <string name="pref_color_theme_value">#87CEEB</string>

    <string-array name="pref_color_theme_entries">
        <item>Black</item>
        <item>Sky Blue</item>
        <item>Red</item>
        <item>White</item>
    </string-array>
    
    <string-array name="pref_color_theme_values">
        <item>#000000</item>
        <item>#87CEEB</item>
        <item>#FF0000</item>
        <item>#FFFFFF</item>
    </string-array>
    
    <string name="pref_group_admin">Admin</string>
    <string name="pref_group_appearance">Appearance</string>

</resources>

Run the application

You can now see the ADMIN and APPEARANCE groups.

Settings Grouped By Title

Settings Grouped By Title

Download the source code

This was an example of grouping settings by title. You can download the source code here: preferencesGroupByTitleExample.zip

Share.

Comments are closed.