Example of Android Preferences

0

Every app needs some kind of settings that allows users to modify app’s features and behavior. Android has built-in support for settings. One can build the settings using use Android’s Preference API. If you want to provide settings for your app, you should use Android’s Preference APIs. In this example I will show you how to build the settings using android preferences.

Create your preferences XML

In this example, we will add a checkbox setting, a text setting to set a value and a list setting which allows one to select a value from the list.
Add an XML file of type Preferences in the res/xml folder.

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>

Add the values in strings.xml. Use string-array to add a list of values.

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>

</resources>

Add Settings to menu

Create an XML file of type menu in the res/menu folder. Add an item element for settings activity. Set showAsAction to never so that it always appears 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>

Inflate preferences XML in your activity class

To display your settings in an activity, you need to extend android provided PreferenceActivity class which in turn extends Activity class. The PreferenceActivity knows how to persist the settings associated with each Preference when the user makes a change.

Call addPreferencesFromResource to set the preferences XML.

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 to overflow menu

Override onCreateOptionsMenu to inflate menu.xml and add it to the menu. Override onOptionsItemSelected to open settings screen as you click on settings item.
You will know the item clicked comparing the item id. Call startActivity(new Intent(this, PreferencesExampleActivity.class)) to start the settings screen.

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

Click on overflow menu to view Settings item

Settings Menu Item

Click on overflow menu to view Settings

Click on Settings to view settings menu.

Settings Screen

Settings Screen

If you need to set a value, use Edit preference

EditPreference

Set List Size using EditPreference

If you need to select a value  from a list of values use a ListPreference

ListPreference

CheckboxPreference

Download Source Code

This was an example of android preferences. You can download the source code here: preferencesExample.zip

Share.

Comments are closed.