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!



2 responses

21 09 2012
SharedPreferences not saved after app restart « Looks OK!

[…] So to solve this problem You need to write code like this (compare it with snippets in previous post): […]

31 05 2013
develop iphone apps

Hi there, just wanted to say, I enjoyed this blog post. It was inspiring.
Keep on posting!

Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: