Shared Preference In Android | Shared Preference In Android Studio | What Is Shared Preference In Android

What is SharedPreferences?

Android provides many ways of storing data of an application. One of the way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of a key-value pair and provides simple methods to read and write them.

Why we use SharedPreferences?

The primary purpose of shared preferences is to store user-specified configuration details, such as user-specific settings, keeping the user logged into the application.

If you don't need to store a lot of data and it doesn't require structure, you should use SharedPreferences.

Think of a situation where you wanna save a small value that you wanna refer later sometime when a user launches the application. Then shared preference comes into action.

Use shared preferences only when you need to save a small amount of data as simple key/value pairs. To manage larger amounts of persistent app data, use other methods such as SQL databases, which you will learn about in later videos.

How to use SharedPreferences?

In android, we can save the preferences data either in single or multiple files based on our requirements.

Android stores Shared Preferences settings as an XML file in shared_prefs folder under DATA/data/{application package} directory.

In case if we use a single file to save the preferences, then we need to use getPreferences() method. And for multiple files, we need to call a getSharedPreferences() method and pass a filename as a parameter.

If we use single file then initialization will go like this:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);

And for multiple files, we need to initialize the object by using getSharedPreferences() method like this:
SharedPreferences sharedPref = getSharedPreferences("FileName",Context.MODE_PRIVATE);

Here, the name FileName is the preference file, and the context mode MODE_PRIVATE, will make sure that the file can be accessed only within our application.

There are three types of Mode in Shared Preference:
1. Context.MODE_PRIVATE – default value (Not accessible outside of your application)
2. Context.MODE_WORLD_READABLE – readable to other apps.
3. Context.MODE_WORLD_WRITEABLE – read/write to other apps.

Please make sure to follow recommendation by Google while giving preference file name. Google recommends to give name as application package name + preferred name.

For example: if your application package name is then preference could be where is the name of preference file.

Write to Shared Preferences:
To store data in shared preference file, we need an editor to edit and save the changes in SharedPreferences object.

SharedPreferences pref= getPreferences(Context.MODE_PRIVATE);
SharedPreferences pref= getSharedPreferences(“”,Context.MODE_PRIVATE);

SharedPreferences.Editor editor = pref.edit();
editor.putString("keyname","string value");
editor.putInt("keyname","int value");
editor.putFloat("keyname","float value");
editor.putLong("keyname","long value");

Only primitive data types can be added in preference file.
to save all the changes we’ll use commit() method.


Read from Shared Preferences: To read or retrieve a values from Shared Preferences file, we need to call a methods such as getInt(), getString(), etc.

SharedPreferences pref= getSharedPreferences(“”,Context.MODE_PRIVATE);


In second parameter we give default value to receive in case of ‘data not found’.

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

Deleting Values:
SharedPreferences.Editor editor = pref.edit();

Deleting Everything:
SharedPreferences.Editor editor = pref.edit();


Post a Comment