Android SharedPreferences

Android SharedPreferences

Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.

In order to use shared preferences , you have to call a method getSharedPreferences() that returns a SharedPreference instance poiting to the file that contains the values of preferences.

The first parameter is the key and the second parameter is the MODE. Apart from private there are other modes availaible that are listed below:

ModeDescription
MODE_APPENDThis will append the new preferences with the already existing preferences.
MODE_ENABLE_WRITE_AHEAD_LOGGINGDatabase open flag. When it is set , it would enable write ahead logging by default.
MODE_MULTI_PROCESSThis method will check for modification of preferences even if the sharedpreference instance has already been loaded.
MODE_PRIVATEBy setting this mode , the file can only be accessed using calling application.
MODE_WORLD_READABLEThis mode allow other application to read the preferences.
MODE_WORLD_WRITEABLThis mode allow other application to write the preferences.

You can save something in the sharedpreferences by using SharedPreferences.Editor class. You will call the edit method of SharedPreference instance and will recieve it in an editor object. Its syntax is:

Apart from the putString method , there are methods availaible in the editor class that allows manipulation of data inside shared preferences. They are listed as follows:

MethodDescription
apply()It is an abstract method. It will commit your changes back from editor to the sharedPreference object you are calling.
clear()It will remove all values from the editor.
remove(String key)It will remove the value whose key has been passed as a parameter.
putLong(String key, long value)It will save a long value in a preference editor.
putInt(String key, int value)It will save a integer value in a preference editor.
putFloat(String key, float value)It will save a float value in a preference editor.

Example

In this example i will be demonstrating how to work on SharedPreferences for storing values in it and fetching again the values.

VIDEO DEMO

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)

2. Open your your AndroidManifest.xml file and make your “MainActivity” as Launcher activity.

3. Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml.

4. Add the following code in MainActivity.java activity. In this following code we use editor to store data in shared preferences on button click and fetch data from shared preferences and finally deleting/clearing shared preferences.

5. Now, run the application and you will get the output as shown in video.

Thanks. 🙂

Download Source Code – >

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)