Android tutorial: app preferences screen with shared preferences

23 06 2012

If there is a need to give some configuration options to the user, you can implement application settings screen, along with saving these settings for application. Settings will be stored in Shared Preferences – place where it will persist and can be read in future. This is a set of key-value pairs, that are stored even after application is closed.

Application settings screen, well known by the Android users looks like this:

1. Create Java class helping you to read and write Shared Preferences

In this tutorial there is just one preference to be set by the user. The class wil contain two methods: one for reading the setting from shared preferences, second one for storing the updated value (shared preferenceĀ getter and setter):

public class AppSettingsHelper {

	public static boolean getPushEnabledFlag(Context context) {
	        SharedPreferences prefs = context.getSharedPreferences(Constants.SP_MY_APPLICATION_SETTINGS, 0);
	        return prefs.getBoolean(

	public static void setPushEnabledFlag(Context context, boolean newValue) {
	        SharedPreferences prefs = context.getSharedPreferences(Constants.SP_MY_APPLICATION_SETTINGS, 0);
	        Editor prefsEditor = prefs.edit();

2. Add new activity with preferences

Create new activity, where all preferences will be shown. Make it extending PreferencesActivity, not the Activity as usual. Add new Activity entry to AndroidManifest.xml. Here is how it looks like in my application:

public class SettingsActivity extends PreferenceActivity {

    protected void onCreate(Bundle savedInstanceState) {

Here I am reading shared preference for this application (under the Constants.SP_MY_APPLICATION_SETTINGS key), and adding a new preferences from the resource. this is the whole code of my activity. Now what about resources?

3. Add settings options to the Preferences activity

Preferences that will be displayed on the screen are customized in xml file: res/xml/prefs.xml. Here I add one preference with checkbox:

<PreferenceScreen xmlns:android="" >
    <PreferenceCategory android:title="@string/settingsActivity_titile">
            android:key="@string/settingsActivity_ReceivePushNotifications_key" />

title and summary are self-explanatory,and the android:key attribute is the name of preference stored in Shared Preferences.

4. How to launch Preferences activity?

As you wish. It can be lanched for example by some button in application, but the most intuitive way to do so is application menu called after the ‘menu’ button press. How to build such menu and call the activity from it, I described here: Android menu implementationĀ tutorial.

My tutorial and implementation is inspired by very good article here.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!


%d bloggers like this: