Monday, May 27, 2024
HomeAndroidAndroid SharedPreferences Example - Android Storage Types Tutorial #1

Android SharedPreferences Example – Android Storage Types Tutorial #1


Android SharedPreferences provides an easy and efficient way to store data in the form of Key value pairs in Android. Unlike other storage options whose setup is a bit complex, SharedPreferences provides an easy way to read and write data into a file.

What are SharedPreferences?

SharedPreferences are just key value pairs which are stored in an xml file. If we know the key, we can access the value.

android sharedpreferences

What kind of data can I store using Android SharedPreferences?

We can store all primitive data types like int, float, long, boolean, String in SharedPreferences. For data that is complex and bulkier than this, it is better to use sqlite, which we’ll also discuss in my future tutorials.

Accessing Android SharedPreferences

Accessing Android SharedPreferences can be done by using the following code:

//Accessing android sharedpreferences
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences("MY_KEY", Context.MODE_PRIVATE);

We can observe that from the above lines of code, getSharedPreferences() method from context takes in two parameters:

  • Key
  • Mode

Key – It is the one which is used to save the datatype in SharedPreferences

Mode – Which sets whether the data should be visible to other apps. MODE_PRIVATE is the default mode.

Different Types of Modes Available:


File creation mode: for use with openFileOutput(String, int), if the file already exists then write data to the end of the existing file instead of erasing it.


Database open flag: when set, the database is opened with write-ahead logging enabled by default.


File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).


Database open flag: when set, the database is opened without support for localized collators.

Writing data to Android SharedPreferences

To write data to Android sharedPreferences we need to create SharedPreferences.Editor by calling edit() method from SharedPreferences as follows:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("MY_KEY", "Vamsi Tallapudi");

Reading data from Android SharedPreferences

//reading data from android sharedPreferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String myName = sharedPref.getString("MY_KEY", "my default value");


So where is my data stored?

The data for the respective app will be stored in the following directory:


Example Please:

Here is the video so that it would be easier to understand Android SharedPreferences with an example.


The sourcecode for the example created in the above Youtube Video on Android SharedPreferences is in the below link pointing to github.



This completes our tutorial on SharedPreferences. We’ll discuss about various other storage options available in Android in upcoming articles.



Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.


3 Sum | Leetcode #15 | 100 Days of Code Day 10

Today we are going to solve 3 Sum - our first Medium Level Array problem from Coderefer DSA Sheet. If you are new to this...

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Follow us


Most Popular