A PreferenceScreen saves user preferences in SharedPreferences. To create a PreferenceScreen, you need a few things:
An XML file to define the available options:
This goes in /res/xml/preferences.xml, and for the above settings screen, it looks like this:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"><PreferenceCategory
android:title="General options"><CheckBoxPreference
android:key ="silent_mode"
android:defaultValue="false"
android:title="Silent Mode"
android:summary="Mute all sounds from this app"/><SwitchPreference
android:key="awesome_mode"
android:defaultValue="false"
android:switchTextOn="Yes"
android:switchTextOff="No"
android:title="Awesome mode™"
android:summary="Enable the Awesome Mode™ feature"/><EditTextPreference
android:key="custom_storage"
android:defaultValue="/sdcard/data/"
android:title="Custom storage location"
android:summary="Enter the directory path where you want data to be saved. If it does not exist, it will be created."
android:dialogTitle="Enter directory path (eg. /sdcard/data/ )"/></PreferenceCategory></PreferenceScreen>
This defines the available options in the settings screen. There are many other types of Preference listed in the Android Developers documentation on the Preference Class(opens new window).
Next, we need an Activity to host our Preferences user interface. In this case, it's quite short, and looks like this:
It extends PreferenceActivity, and provides the user interface for the preferences screen. It can be started just like a normal activity, in this case, with something like:
Intent i =newIntent(this,PreferencesActivity.class);startActivity(i);
Don't forget to add PreferencesActivity to your AndroidManifest.xml.
Getting the values of the preferences inside your app is quite simple, just call setDefaultValues() first, in order to set the default values defined in your XML, and then get the default SharedPreferences. An example:
//set the default values we defined in the XMLPreferenceManager.setDefaultValues(this,R.xml.preferences,false);SharedPreferences preferences =PreferenceManager.getDefaultSharedPreferences(this);//get the values of the settings optionsboolean silentMode = preferences.getBoolean("silent_mode",false);boolean awesomeMode = preferences.getBoolean("awesome_mode",false);String customStorage = preferences.getString("custom_storage","");
Obviously, you should call either apply() or commit().
SharedPreferences settings =getSharedPreferences(PREFS_FILE, MODE_PRIVATE);SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(PREF_CONST,true);// This will asynchronously save the shared preferences without holding the current thread.
editor.apply();
SharedPreferences settings =getSharedPreferences(PREFS_FILE, MODE_PRIVATE);SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(PREF_CONST,true);// This will synchronously save the shared preferences while holding the current thread until done and returning a success flag.boolean result = editor.commit();
apply() was added in 2.3 (API 9), it commits without returning a boolean indicating success or failure.
commit() returns true if the save works, false otherwise.
apply() was added as the Android dev team noticed that almost no one took notice of the return value, so apply is faster as it is asynchronous.
Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits(apply) are completed as well as any other sync commits that may be pending.
Read and write values to SharedPreferences
publicclassMyActivityextendsActivity{privatestaticfinalString PREFS_FILE ="NameOfYourPrefrenceFile";// PREFS_MODE defines which apps can access the fileprivatestaticfinalint PREFS_MODE =Context.MODE_PRIVATE;// you can use live template "key" for quickly creating keysprivatestaticfinalString KEY_BOOLEAN ="KEY_FOR_YOUR_BOOLEAN";privatestaticfinalString KEY_STRING ="KEY_FOR_YOUR_STRING";privatestaticfinalString KEY_FLOAT ="KEY_FOR_YOUR_FLOAT";privatestaticfinalString KEY_INT ="KEY_FOR_YOUR_INT";privatestaticfinalString KEY_LONG ="KEY_FOR_YOUR_LONG";@OverrideprotectedvoidonStart(){super.onStart();// Get the saved flag (or default value if it hasn't been saved yet)SharedPreferences settings =getSharedPreferences(PREFS_FILE, PREFS_MODE);// read a boolean value (default false)boolean booleanVal = settings.getBoolean(KEY_BOOLEAN,false);// read an int value (Default 0)int intVal = settings.getInt(KEY_INT,0);// read a string value (default "my string")String str = settings.getString(KEY_STRING,"my string");// read a long value (default 123456)long longVal = settings.getLong(KEY_LONG,123456);// read a float value (default 3.14f)float floatVal = settings.getFloat(KEY_FLOAT,3.14f);}@OverrideprotectedvoidonStop(){super.onStop();// Save the flagSharedPreferences settings =getSharedPreferences(PREFS_FILE, PREFS_MODE);SharedPreferences.Editor editor = settings.edit();// write a boolean value
editor.putBoolean(KEY_BOOLEAN,true);// write an integer value
editor.putInt(KEY_INT,123);// write a string
editor.putString(KEY_STRING,"string value");// write a long value
editor.putLong(KEY_LONG,456876451);// write a float value
editor.putFloat(KEY_FLOAT,1.51f);
editor.apply();}}
Retrieve all stored entries from a particular SharedPreferences file
The getAll()(opens new window) method retrieves all values from the preferences. We can use it, for instance, to log the current content of the SharedPreferences:
The documentation warns you about modifying the Collection returned by getAll:
Note that you must not modify the collection returned by this method,
or alter any of its contents. The consistency of your stored data is
not guaranteed if you do.
Reading and writing data to SharedPreferences with Singleton
SharedPreferences Manager (Singleton) class to read and write all types of data.
importandroid.content.Context;importandroid.content.SharedPreferences;importandroid.util.Log;importcom.google.gson.Gson;importjava.lang.reflect.Type;/**
* Singleton Class for accessing SharedPreferences,
* should be initialized once in the beginning by any application component using static
* method initialize(applicationContext)
*/publicclassSharedPrefsManager{privatestaticfinalString TAG =SharedPrefsManager.class.getName();privateSharedPreferences prefs;privatestaticSharedPrefsManager uniqueInstance;publicstaticfinalString PREF_NAME ="com.example.app";privateSharedPrefsManager(Context appContext){
prefs = appContext.getSharedPreferences(PREF_NAME,Context.MODE_PRIVATE);}/**
* Throws IllegalStateException if this class is not initialized
*
* @return unique SharedPrefsManager instance
*/publicstaticSharedPrefsManagergetInstance(){if(uniqueInstance ==null){thrownewIllegalStateException("SharedPrefsManager is not initialized, call initialize(applicationContext) "+"static method first");}return uniqueInstance;}/**
* Initialize this class using application Context,
* should be called once in the beginning by any application Component
*
* @param appContext application context
*/publicstaticvoidinitialize(Context appContext){if(appContext ==null){thrownewNullPointerException("Provided application context is null");}if(uniqueInstance ==null){synchronized(SharedPrefsManager.class){if(uniqueInstance ==null){
uniqueInstance =newSharedPrefsManager(appContext);}}}}privateSharedPreferencesgetPrefs(){return prefs;}/**
* Clears all data in SharedPreferences
*/publicvoidclearPrefs(){SharedPreferences.Editor editor =getPrefs().edit();
editor.clear();
editor.commit();}publicvoidremoveKey(String key){getPrefs().edit().remove(key).commit();}publicbooleancontainsKey(String key){returngetPrefs().contains(key);}publicStringgetString(String key,String defValue){returngetPrefs().getString(key, defValue);}publicStringgetString(String key){returngetString(key,null);}publicvoidsetString(String key,String value){SharedPreferences.Editor editor =getPrefs().edit();
editor.putString(key, value);
editor.apply();}publicintgetInt(String key,int defValue){returngetPrefs().getInt(key, defValue);}publicintgetInt(String key){returngetInt(key,0);}publicvoidsetInt(String key,int value){SharedPreferences.Editor editor =getPrefs().edit();
editor.putInt(key, value);
editor.apply();}publiclonggetLong(String key,long defValue){returngetPrefs().getLong(key, defValue);}publiclonggetLong(String key){returngetLong(key,0L);}publicvoidsetLong(String key,long value){SharedPreferences.Editor editor =getPrefs().edit();
editor.putLong(key, value);
editor.apply();}publicbooleangetBoolean(String key,boolean defValue){returngetPrefs().getBoolean(key, defValue);}publicbooleangetBoolean(String key){returngetBoolean(key,false);}publicvoidsetBoolean(String key,boolean value){SharedPreferences.Editor editor =getPrefs().edit();
editor.putBoolean(key, value);
editor.apply();}publicbooleangetFloat(String key){returngetFloat(key,0f);}publicbooleangetFloat(String key,float defValue){returngetFloat(key, defValue);}publicvoidsetFloat(String key,Float value){SharedPreferences.Editor editor =getPrefs().edit();
editor.putFloat(key, value);
editor.apply();}/**
* Persists an Object in prefs at the specified key, class of given Object must implement Model
* interface
*
* @param key String
* @param modelObject Object to persist
* @param <M> Generic for Object
*/public<MextendsModel>voidsetObject(String key,M modelObject){String value =createJSONStringFromObject(modelObject);SharedPreferences.Editor editor =getPrefs().edit();
editor.putString(key, value);
editor.apply();}/**
* Fetches the previously stored Object of given Class from prefs
*
* @param key String
* @param classOfModelObject Class of persisted Object
* @param <M> Generic for Object
* @return Object of given class
*/public<MextendsModel>MgetObject(String key,Class<M> classOfModelObject){String jsonData =getPrefs().getString(key,null);if(null!= jsonData){try{Gson gson =newGson();M customObject = gson.fromJson(jsonData, classOfModelObject);return customObject;}catch(ClassCastException cce){Log.d(TAG,"Cannot convert string obtained from prefs into collection of type "+
classOfModelObject.getName()+"\n"+ cce.getMessage());}}returnnull;}/**
* Persists a Collection object in prefs at the specified key
*
* @param key String
* @param dataCollection Collection Object
* @param <C> Generic for Collection object
*/public<C>voidsetCollection(String key,C dataCollection){SharedPreferences.Editor editor =getPrefs().edit();String value =createJSONStringFromObject(dataCollection);
editor.putString(key, value);
editor.apply();}/**
* Fetches the previously stored Collection Object of given type from prefs
*
* @param key String
* @param typeOfC Type of Collection Object
* @param <C> Generic for Collection Object
* @return Collection Object which can be casted
*/public<C>CgetCollection(String key,Type typeOfC){String jsonData =getPrefs().getString(key,null);if(null!= jsonData){try{Gson gson =newGson();C arrFromPrefs = gson.fromJson(jsonData, typeOfC);return arrFromPrefs;}catch(ClassCastException cce){Log.d(TAG,"Cannot convert string obtained from prefs into collection of type "+
typeOfC.toString()+"\n"+ cce.getMessage());}}returnnull;}publicvoidregisterPrefsListener(SharedPreferences.OnSharedPreferenceChangeListener listener){getPrefs().registerOnSharedPreferenceChangeListener(listener);}publicvoidunregisterPrefsListener(SharedPreferences.OnSharedPreferenceChangeListener listener){getPrefs().unregisterOnSharedPreferenceChangeListener(listener);}publicSharedPreferences.EditorgetEditor(){returngetPrefs().edit();}privatestaticStringcreateJSONStringFromObject(Object object){Gson gson =newGson();return gson.toJson(object);}}
Modelinterface which is implemented by classes going to Gson to avoid
proguard obfuscation.
The listener will fire only if value was added or changed, setting the same value won't call it;
The listener needs to be saved in a member variable and NOT with an anonymous class, because registerOnSharedPreferenceChangeListener stores it with a weak reference, so it would be garbage collected;
Instead of using a member variable, it can also be directly implemented by the class and then call registerOnSharedPreferenceChangeListener(this);
Remember to unregister the listener when it is no more required using unregisterOnSharedPreferenceChangeListener.
getPreferences(int) VS getSharedPreferences(String, int)
getPreferences(int)
returns the preferences saved by Activity's class name as described in the docs(opens new window) :
Retrieve a SharedPreferences object for accessing preferences that are private to this activity. This simply calls the underlying getSharedPreferences(String, int) method by passing in this activity's class name as the preferences name.
Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values.
So if the value being saved in the SharedPreferences has to be used across the app, one should use getSharedPreferences (String name, int mode) with a fixed name. As, using getPreferences(int) returns/saves the preferences belonging to the Activity calling it.
Store, Retrieve, Remove and Clear Data from SharedPreferences
editor.remove("key_name3");// will delete key key_name3
editor.remove("key_name4");// will delete key key_name4// Save the changes in SharedPreferences
editor.commit();// commit changes
After the apply(), prefs contains "key" -> "value", in addition to whatever it contained already. Even though it looks like I added "key" and then removed it, the remove actually happens first. The changes in the Editor are all applied in one go, not in the order you added them. All removes happen before all puts.
Different ways of instantiating an object of SharedPreferences
// Note that the other app must declare prefs as MODE_WORLD_WRITEABLEfinalArrayList<HashMap<String,String>> LIST =newArrayList<HashMap<String,String>>();Context contextOtherApp =createPackageContext("com.otherapp",Context.MODE_WORLD_WRITEABLE);SharedPreferences prefs = contextOtherApp.getSharedPreferences("pref_file_name",Context.MODE_WORLD_READABLE);
Supported data types in SharedPreferences
SharedPreferences allows you to store primitive data types only (boolean, float, long, int, String, and string set). You cannot store more complex objects in SharedPreferences, and as such is really meant to be a place to store user settings or similar, it's not meant to be a database to keep user data (like saving a todo list a user made for example).
To store something in SharedPreferences you use a Key and a Value. The Key is how you can reference what you stored later and the Value data you want to store.
String keyToUseToFindLater = "High Score";
int newHighScore = 12938;
//getting SharedPreferences & Editor objects
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
//saving an int in the SharedPreferences file
editor.putInt(keyToUseToFindLater, newHighScore);
editor.commit();
Add filter for EditTextPreference
Create this class :
publicclassInputFilterMinMaximplementsInputFilter{privateint min, max;publicInputFilterMinMax(int min,int max){this.min = min;this.max = max;}publicInputFilterMinMax(String min,String max){this.min =Integer.parseInt(min);this.max =Integer.parseInt(max);}@OverridepublicCharSequencefilter(CharSequence source,int start,int end,Spanned dest,int dstart,int dend){try{int input =Integer.parseInt(dest.toString()+ source.toString());if(isInRange(min, max, input))returnnull;}catch(NumberFormatException nfe){}return"";}privatebooleanisInRange(int a,int b,int c){return b > a ? c >= a && c <= b : c >= b && c <= a;}}
- public SharedPreferences getSharedPreferences(String name, int mode)
Activity Method
public SharedPreferences getPreferences()
SharedPreferences Methods
public SharedPreferences.Editor edit()
public boolean contains()
public Map<String, ?> getAll()
public boolean getBoolean(String key,
boolean defValue)
public float getFloat(String key,
float defValue)
public int getInt(String key,
int defValue)
public long getLong(String key,
long defValue)
public String getString(String key,
String defValue)
public Set getStringSet(String key,
Set defValues)
public void registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
public void unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
SharedPreferences.Editor Methods
public void apply()
public boolean commit()
public SharedPreferences.Editor clear()
public SharedPreferences.Editor putBoolean (String key,
boolean value)
public SharedPreferences.Editor putFloat (String key,
float value)
public SharedPreferences.Editor putInt (String key,
int value)
public SharedPreferences.Editor putLong (String key,
long value)
public SharedPreferences.Editor putString (String key,
String value)
public SharedPreferences.Editor putStringSet (String key,
Set values)
public SharedPreferences.Editor remove (String key)
Parameters
Parameter
Details
key
A non-null String identifying the parameter. It can contain whitespace or non-printables. This is only used inside your app (and in the XML file), so it doesn't have to be namespaced, but it's a good idea to have it as a constant in your source code. Don't localize it.
defValue
All the get functions take a default value, which is returned if the given key is not present in the SharedPreferences. It's not returned if the key is present but the value has the wrong type: in that case you get a ClassCastException.
Remarks
`SharedPreferences` shouldn't be used for storing large amount of data. For such purposes, it's much better to use `SQLiteDatabase`.
`SharedPreferences` are single process only, unless you use deprecated mode `MODE_MULTI_PROCESS`. So if your app has multiple processes, you won't be able to read main process's `SharedPreferences` in another process.
In such cases, you should use another mechanism to share data across processes, but don't use `MODE_MULTI_PROCESS` as it is not reliable as well as deprecated.
It's better to use `SharedPreferences` instance in `Singleton` class to access all over the Application `context`. If you want to use it only for particular Activity go for `getPreferences()`.
Avoid storing sensitive information in clear text while using `SharedPreferences` since it can be read easily.