Open In App

How to View Data Stored in Shared Preferences in Android Studio?

Last Updated : 24 May, 2021
Like Article

Shared Preferences in Android is local storage where we can save our data using a key and value pair. It is generally used to store data in users’ devices. Shared Preferences is also used for session management in Android apps where we are using login functionality. In this article, we will take a look at how we can view the data store in Shared Preferences in Android Studio

What we are going to build in this article? 

We will be building a simple application in which we will be simply saving data in shared preferences using a simple EditText field. After that, we will see this data stored in this file in Android Studio. 

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--Edit text for getting user input-->
        android:hint="Enter your message" />
    <!--button for saving data-->
        android:text="Save Data"
        android:textAllCaps="false" />



Step 3: Working with the file


Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.



package com.example.gfgpagination;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    // creating constant keys for shared preferences.
    public static final String SHARED_PREFS = "shared_prefs";
    // key for storing email.
    public static final String MESSAGE_KEY = "message_key";
    // variable for shared preferences.
    SharedPreferences sharedpreferences;
    // creating variables for our edit text and button.
    private EditText messageEdt;
    protected void onCreate(Bundle savedInstanceState) {
        // getting the data which is stored in shared preferences.
        sharedpreferences = getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
        // initializing our variables.
        messageEdt = findViewById(;
        Button saveBtn = findViewById(;
        // adding on click listener for our button.
        saveBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // inside on click we are checking if the entered data
                // by user is empty or not.
                String msg = messageEdt.getText().toString();
                if (TextUtils.isEmpty(msg)) {
                    // if the input is empty we are displaying a toast message.
                    Toast.makeText(MainActivity.this, "Please enter your message", Toast.LENGTH_SHORT).show();
                } else {
                    // if the input is not empty we are calling a method to save
                    // data to shared prefs.
    private void saveMessage(String msg) {
        SharedPreferences.Editor editor = sharedpreferences.edit();
        // below lines will put values for
        // message in shared preferences.
        editor.putString(MESSAGE_KEY, msg);
        // to save our data with key and value.
        // on below line we are displaying a toast message after adding data to shared prefs.
        Toast.makeText(this, "Message saved to Shared Preferences", Toast.LENGTH_SHORT).show();
        // after that we are setting our edit text to empty



Now run your application and add some data in the edit text filed and click on the button to save data to shared preferences. You can get to see the process in the video below  : 





Step 4: View the data stored in shared preferences


Now inside Android studio in the right bottom corner, you will get to see the option as Device File Explorer. Click on that option you will get to see the below screen. Inside this screen Navigate to data > data folder.



Step 5: Opening the file containing shared preferences 


Now inside that check for your app’s package name. Click on the package name for your application. After that click on the shared_prefs folder and inside that open the shared_prefs.xml file. Now you will get to see the data which we stored in our shared preferences from our application.     



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads