Setup the Preferences

Android preferences is a key/value entries that store data that can be specific to a certain activity or shared among all activities within the application. 

the data are stored in a xml file.

  1. Preferences can be retrieved only by a single activity, we do it like this: we get a SharedPreferences object by calling getPreferences(int mode)

SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor=prefs.edit(); editor.putString(“pref 1”, “some text”); editor.commit();

  • the mode value can be one of the following:
      1. Context.MODE_PRIVATE (0): a file creating mode that makes the created file only accessible by applications with the same user ID (access the file from the same application context, will desctribe later).
      2. Context.MODE_WORLD_READABLE (1): file mode makes the file readable from other applications.
      3. Context.MODE_WORLD_WRITEABLE (2): file mode allows other applications to write to the file.

we get an instance of SharedPreferences.Editor and write the preference value with editor.putString(String key, String value) method.
shared preferences allows you to insert preferences using the following methods:

  1. editor.putBoolean(String key, boolean value).
  2. editor.putFloat(String key,float value).
  3. editor.putInt(String key, int value).
  4. editor.putLong(String key, long value)
  5. editor.putString(String key, String value)

then we call edit.commit() to save the preferences to the file. commit returns a boolean indicating the result of saving, true if successful and false if failed.

It can be shared and retrieved among all activities within the application, or through all applications on the device. To save preferences that can be retrieved from all activities in the application we do it like this:

SharedPreferences prefs= getSharedPreferences("demopref", Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor editor=prefs.edit();
        editor.putString("demostring", "hello");

we give our preferences file a name (demopref in this case) so that other activities can reference that preferences file.
Sharing preferences across applications: 

We can store preferences in one application and read them in another application, this is done reading the preferences file by loading them through the first application’s context. let’s assume we have two applications:

  1. Application 1 with package name “com.mina.prefdemo“.
  2. Application2 with package name “com.mina.demoapp“.
If application1 creates a preferences file with the name “demopref” and inserts a String preference with the key/value “demostring/hello”.
now we access this file and value from application 2 like this:
Context con;
  try {
   con = createPackageContext("com.minasamy.prefdemo", 0);
   SharedPreferences pref=con.getSharedPreferences("demopref", Context.MODE_PRIVATE);
   String x=pref.getString("demostring", "not found");
  } catch (NameNotFoundException e) {
   Log.e(Tag, e.toString());

// Setup the Preferences for this application, by class.
    Preferences prefs = Preferences.userNodeForPackage(MainClass.class);

   // Retrieve some preferences previously stored, with defaults in case this is the first run.
    String text = prefs.get(“A”“a”);
    String display = prefs.get(“B”“b”);

// Assume the user chose new preference values: Store them back.


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s


Just another site

Jing's Blog

Just another site

Start from here......







Just another site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision


Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness



Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

John's Weblog

Just another weblog

I Am That I Am

Chasing tomorrow's sunrise.

%d bloggers like this: