117

I'd like to store some application settings (like the URL of an API, and some settings for testing) for an Android application.

I mostly work as a .NET developer, so I'm used to using the file app.config for this purpose. What's a good way to do it in Android?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tim Almond
  • 12,088
  • 10
  • 40
  • 50

1 Answers1

159

Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.

  1. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

  2. Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.

Shared Preferences:

The shared preferences are managed with the help of getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.

(1) Here is how you get the instance when you specify the file name

public static final String PREF_FILE_NAME = "PrefFile";
   SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

(2) The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:

 int storedPreference = preferences.getInt("storedInt", 0);

To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

Editor also support methods like remove() and clear() to delete the preference value from the file.

Activity Preferences:

The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

Following is the code to get preferences

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

The code to store values is also same as in case of shared preferences.

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.

To see some more examples check Android's Data Storage post on developers site.

BlackBear
  • 22,411
  • 10
  • 48
  • 86
Pentium10
  • 204,586
  • 122
  • 423
  • 502
  • 1
    thanks. Looks like what I need to do is to use an XML file for what I'm after (or maybe I use the XML as the data to intially load), but the preference stuff is useful for another part of my project. – Tim Almond Aug 26 '10 at 00:42
  • 1
    You can use the xml file for init. You need to call something like `PreferenceManager.setDefaultValues(this, R.xml.profiles_preferences, false);` – Pentium10 Aug 26 '10 at 06:57
  • Wonderful answer. Please note, that the class is PreferenceManager not Preference(s)Manager. – Jade Jun 14 '13 at 02:39
  • 3
    This guide could be useful too: http://developer.android.com/guide/topics/ui/settings.html – Hung Tran Jan 03 '15 at 02:38
  • 2
    +1. Just one tiny bit to keep in mind. After setting the value in `preferences` object, calling `editor.apply()` is recommended over `editor.commit()` as the former will handle the saving in background. – Anindya Chatterjee Aug 31 '15 at 13:41
  • 2
    I am bit confused here. The sharedPreferences is for storing user settings over the app, not apps settings. The user (and me also) was wondering where to store app settings , ex API url or other settings that the app use to work properly. – albanx Feb 23 '16 at 23:32
  • @albanx , I store API url in a CONSTANTS interface something like , public BASE_API_URL = "http://....." – charany1 May 31 '16 at 16:52
  • Consider using https://github.com/UdiOshi85/GenericSettings for fast implementation – Udi Oshi Nov 13 '17 at 10:21