Android SharedPreferences Example – Writing and Reading Values

SharedPreferences are using Android to store data in Key-Value pairs. In this post, we will see simple Android SharedPreferences Example. We will learn saving values to SharedPreferences, and Reading them back in the application. We will also see how to update and delete the values in SharedPreferences.

Android SharedPreferences

We have a class named SharedPreferences in android. It allows us a framework that helps in saving and retrieving persistent key-value pairs of primitive datatypes. So remember the limitations of using Android SharedPreferences,  it only allows primitive data types. The values are saved even when the application is killed.

When to use SharedPreferences?

As the name suggests, it is used to store preferences, i.e., user-specific data, like settings, login sessions, etc. You may find this Android Login and Registration tutorial helpful where we are using SharedPreferences to maintain a user Login Session.

Accessing SharedPreferences

Accessing SharedPreferences is very easy we can use the following methods, and it will give us an instance of the SharedPreferences class.

SharedPreferences getSharedPreferences(String, int)This method can be called from inside an Activity or the Context object.

It takes two parameters, the first parameter is a String, and it is the name of the SharedPreferences that we want to get. If we already have a SharedPreferences of the specified name it will return us that. But if we don’t have any existing SharedPrferences of the specified name it will create one and will return the instance of SharedPreferences class. The second parameter defines the mode of the preference, and it can be private to the activity or shared.

We use this method when we want multiple SharedPreferences for our application, specified by different name. 

 SharedPreferences getPreferences(int mode)It does the same thing as getSharedPreferences(), but here we don’t need to supply a name. We use it only when we want a Single SharedPreferences that is the default one for the application.

SharedPreferences Modes

MODE_PRIVATEThis mode will make the shared preferences private to the application.
MODE_WORLD_READABLEAs the name suggest it is world readable, and it might be the reason for the security loop holes in your application. So you should use this mode with extra care.
MODE_WORLD_WRITEABLEAs the name suggest it is world writable, so it is even more dangerous then the world readable mode.  So you should use this mode with extra care.
MODE_MULTI_PROCESSIt is deprecated from API 23, so we should not use it..
MODE_APPENDIf you don’t want to destroy your existing preferences you can use this mode to append the preferences with your existing one.

Saving Values to SharedPreferences

Now lets see how do we save values in SharedPreferences. Remember only primitive data types can be saved. (Though with a little trick you can save anything).

We have the following steps to save values in SharedPreferences.

  1. Get the SharedPreferences
  2. Initialize the Editor
  3. Put the values
  4. Apply the changes

#1 Get the SharedPreferences

  • Inside your activity we will call the method getSharedPreferences().

#2 Initializing the Editor

  • Now the following line will initialize the editor where we will put the values to be saved, in key-value pairs.

#3 Put the values

  • We have the method putDataType() inside the editor. It can be putString, putInt, putChar and you can use the same for every data type that we have. The method takes two arguments first is the key and the second is the respective value. Key is always string and the second parameter is the  data that we want to save. If we are using putString then second parameter would be a String.

#4 Apply the changes

  • Then from the Editor instance we will simply call the apply() method to save the changes.

Getting Values Back from SharedPreferences

Reading values back is very easy. We have only 2 steps.

  1. Get SharedPreferences
  2. Get Values

#1 Get SharedPreferences

This step is same as we did above.

#2 Get Values

  • To get the values we have method getDataType() i.e. getString(), getBoolean() etc.

  • While reading the values with these methods we pass second parameter as the default value. The default value is returned when we don’t have an already saved value for the specified key.

Modifying Values

  • Values are overwritten when we save it again. So to modify a value, simply save it again.

Deleting Values

  • Here we can do two things.
    1. Delete a specific value
    2. Delete everything

#1 Deleting a specific value

  • From the editor instance we will call remove() method by passing the key that is to be removed from SharedPreferences.

#2 Deleting Everything

  • Simply call the method clear() and then apply() from the editor, and it will clear everything.

Android SharedPreferences Example

  • Now let’s try everything that we learned so far, in the Android Studio Project.

Creating a Project

  • Create a new Project. I have created a project named, SharedPrefExample.

Creating User Interface

  • Now we will create a very simple User Interface.
android sharedpreferences example
Android SharedPreferences Example
  • As you can see, we have an EditText to take value that is to be saved in the SharedPreferences. Below the EditText we also have a TextView (not visible as it has no text now) to display the value already saved. Then we have a Button to save value after entering value in the EditText.
  • To create the above given UI, you can use the following xml code.

Saving and Reading Values

  • Write the following code inside

  • Now you can try running the application.
android sharedpreferences example
Android SharedPreferences Example
  • You see it is working fine.

So that’s all for this Android SharedPreferences Example. If you are facing any troubles doing it, then let me know in the comment section. If you found this tutorial helpful, then help us by SHARING this post. Thank You 🙂

4 thoughts on “Android SharedPreferences Example – Writing and Reading Values”

  1. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.

  2. I have developed an android app in firebase database.
    I want to sign up using email and password but while signing in the key should be ask from user and if the key matching in firebase then only the user can register and access the realtime database.
    How can i do this in android studio

  3. I want Like Welcome Belal Khan Will be Shown on next activity.
    Like when i click on save there is Intent of Next Activity. and the Welcome Belal Khan will shown on next activity.


Leave a Comment