Preferences by Rachael Stedman

Updated on: 03.29.2009

The Basics

4 Steps to Adding Preferences to Your Application

XML File

Create a preferences.xml file in your res/xml/ directory. The root of the preference xml is a PreferenceScreen element. Within the PreferenceScreen element you can put preference definitions ( CheckBoxPreference, DialogPreference, ListPreference, EditTextPreference, RingtonePreference, etc.)

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <CheckBoxPreference
                android:key="@string/checkbox"
                android:title="Large Text"
                android:summary="Magnify Text on Screen" />
</PreferenceScreen>

PreferenceActivity

Create a PreferenceActivity. You set up your preferences with the xml file. Then make them accessible to the user through a PreferenceActivity.

   1 public class EditPreferences extends PreferenceActivity { 
   2   @Override 
   3   public void onCreate(Bundle savedInstanceState) { 
   4     super.onCreate(savedInstanceState); 
   5     
   6     addPreferencesFromResource(R.xml.preferences); 
   7   }
   8 }

Don’t forget to add your subclass of PreferenceActivity to your AndroidManifest.xml file.

<activity android:name=".PreferenceActivitySubclass"
          android:label="@string/app_name">
</activity>

Invoke your activity

Invoke the PreferenceActivity such as from a menu option.

   1     @Override
   2     public boolean onCreateOptionsMenu(Menu menu) {
   3         menu.add(Menu.NONE, EDIT_ID, Menu.NONE, "Edit Prefs")
   4                 .setIcon(R.drawable.misc)
   5                 .setAlphabeticShortcut('e');
   6         menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, "Close")
   7                         .setIcon(R.drawable.eject)
   8                         .setAlphabeticShortcut('c');
   9         return(super.onCreateOptionsMenu(menu));
  10         
  11     }
  12     
  13     @Override
  14     public boolean onOptionsItemSelected(MenuItem item) {
  15         switch (item.getItemId()) {
  16                 case EDIT_ID:
  17                         startActivity(new Intent(this, EditPreferences.class));
  18                         return(true);
  19                         
  20                 case CLOSE_ID:
  21                         finish();
  22                         return(true);
  23         }
  24         
  25         return(super.onOptionsItemSelected(item));
  26     }

Access stored preferences

Access stored preferences to reflect user choices in your activity (put in onResume() method).

   1     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
   2         
   3     TextView checkbox = (TextView)findViewById(R.id.textread);
   4     if(prefs.getBoolean(getString(R.string.checkbox), false)) {
   5         checkbox.setTextSize(20);
   6     } else {
   7         checkbox.setTextSize(12);
   8     }

Helpful Links

License

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

http://creativecommons.org/images/public/somerights20.png


CategoryTutorial

FrontPage/Tutorials/Preferences (last edited 2009-03-29 20:39:34 by MarkChang)