Example of Grouping Android Preferences By Sub-Screens

0

In this example, we will group the settings by sub-screen. This will be a continuation of our previous preferences example where we have grouped preferences by title.
If the number of settings are too many user will end up spending more time then needed to just to search that specific setting that he or she wants to change.

If you still have lots of settings, it will be helpful to group related settings together in sub-screens.

Group Preferences using PreferenceScreen

PreferenceScreen represents one preference screen. If you have multiple groups, you can place each one in a separate sub-screen using nested PreferenceScreen.

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" >

    <PreferenceScreen
        android:key="pref_config_settings"
        android:summary="@string/pref_group_configure_summary"
        android:title="@string/pref_group_configure" >
        <PreferenceScreen
            android:key="pref_appearance"
            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" />
        </PreferenceScreen>
        <PreferenceScreen
            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" />
        </PreferenceScreen>
    </PreferenceScreen>
    <PreferenceScreen
        android:key="pref_support_settings"
        android:summary="@string/pref_group_support_summary"
        android:title="@string/pref_group_support" >
        <PreferenceScreen
            android:key="@string/pref_support_about"
            android:title="@string/pref_support_about" />

        <CheckBoxPreference
            android:defaultValue="false"
            android:key="perf_show_help"
            android:title="@string/pref_support_show_help" />
    </PreferenceScreen>

</PreferenceScreen>

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>
    <string name="pref_group_configure">Configuration</string>
    <string name="pref_group_configure_summary">Configuration of admin, appearance</string>
    <string name="pref_group_support">Support</string>
    <string name="pref_group_support_summary">About, Help</string>
    <string name="pref_support_about">About</string>
    <string name="pref_support_show_help">Help</string>

</resources>

Call addPreferencesFromResources(R.xml.preferences) which in turn will inflate preferences.xml and build list of settings.

PreferencesExampleActivity:

package com.javarticles.android;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class PreferencesExampleActivity extends PreferenceActivity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
	}

}

Add settings menu item to the menu.xml to appear in the overflow menu.

menu.xml:

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

    <item
        android:actionViewClass="com.javarticles.android.PreferencesExampleActivity"
        android:showAsAction="never"
        android:title="@string/settings"
        android:id="@+id/settings">
    </item>

</menu>

In MainActivity, override onCreateOptionsMenu to inflate menu.xml. This will build the menu. In onOptionsItemSelected, we start the settings activity when ‘Settings’ menu item is clicked.

MainActivity:

package com.javarticles.android;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.settings:
			startActivity(new Intent(this, PreferencesExampleActivity.class));
		}
		return super.onOptionsItemSelected(item);
	}
}

Run the application

List of main settings:

Main Settings

Main Settings

Configuration settings

Configuration Settings

Configuration Settings

Appearance settings

Appearance Settings

Appearance Settings

Admin settings

Admin Settings

Admin Settings

Support Settings

Support Settings

Support Settings

Download the source code

This was an example of grouping preferences by sub-screens. You can download the source code here: preferencesGroupBySubScreen.zip

Share.

Comments are closed.