Android Tutorial - Android Preferences

Android provides the SharedPreferences object to help you save simple application data.

Using the SharedPreferences object, you save the data you want through the use of name/value pairs.

In the following code, you learn how to use the SharedPreferences object to store application data.


Create a file at res/xml/myapppreferences.xml and populate the myapppreferences.xml file as follows:

         <?xml version="1.0" encoding="utf-8"?>
             <PreferenceCategory android:title="Category 1">
                     android:summary="True or False"
                     android:key="checkboxPref" />
             <PreferenceCategory android:title="Category 2">
                     android:summary="Enter a string"
                     android:defaultValue="[Enter a string here]"
                     android:title="Edit Text"
                     android:key="editTextPref" />
                     android:summary="Select a ringtone"
                     android:key="ringtonePref" />
                     android:title="Second Preference Screen"
                  android:summary= "Click here to go to the second Preference Screen"
                  android:key="secondPrefScreenPref" >
                      android:summary="Enter a string"
                      android:title="Edit Text (second Screen)"
                      android:key="secondEditTextPref" />

Java code

import android.os.Bundle;
import android.preference.PreferenceActivity;
//from w ww  . j a  va  2 s. c om
public class AppPreferenceActivity extends PreferenceActivity {
     public void onCreate(Bundle savedInstanceState) {
         //load the preferences from an XML file


Once you have modified the value of at least one of the preferences, a file is created in the /data/data/com.java2s.your activity name/shared_prefs folder of the Android emulator.