Store and retrive data using sharedpreference in android

29/05/2017   09:33:08 AM

Hello in this android article i will explain what is shared preference in android and how to store the data in to file using shared preference and display that data.

What is SharedPreference ?

1. The SharedPreferences is a built in class used to save and retrieve application data in key-value pairs.

2. It Stores only primitive data types data.(booleans, floats, ints, longs, and strings.)

3.Data is stored in xml file in the directory data/data/pkgname/sharedpreferences folder

4.It is mainly used to store username and password,application settings

 1.Create a new project in android studio/add the code to/res/layout/activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright"
    tools:context=" ">

    <RelativeLayout
        android:layout_width="366dp"
        android:layout_height="179dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="21dp">

        <EditText
            android:id="@+id/edtName"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_above="@+id/edtMailid"
            android:layout_alignLeft="@+id/edtMailid"
            android:layout_alignStart="@+id/edtMailid"
            android:layout_marginBottom="22dp"

            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/edtMailid"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="40dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Mail Id"
            android:textSize="24sp"
            app:layout_constraintHorizontal_bias="0.252"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_editor_absoluteY="100dp" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="366dp"
        android:layout_height="63dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.479">

        <Button
            android:id="@+id/btnLoad"
            android:layout_width="125dp"
            android:layout_height="50dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/btnStore"
            android:layout_marginEnd="75dp"
            android:layout_marginRight="75dp"
            android:text="Load Data"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btnStore"
            android:layout_width="88dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginEnd="48dp"
            android:layout_marginRight="48dp"
            android:layout_toLeftOf="@+id/btnLoad"
            android:layout_toStartOf="@+id/btnLoad"
            android:text="Store"
            android:textSize="18sp" />
    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

 

 

2.Add the following code on MainActivity.java

package com.simplylearnprogramming.sharedpref;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    EditText name,mailid;
    Button save,load;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name= (EditText) findViewById(R.id.edtName);
        mailid= (EditText) findViewById(R.id.edtMailid);
        save= (Button) findViewById(R.id.btnStore);
        load= (Button) findViewById(R.id.btnLoad);
        save.setOnClickListener(this);
        load.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.btnStore)
        {
            //store data using sharedprefernces
            SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            //Edit method allow to write the data in sharedpreferences
            editor.putString("name",name.getText().toString());
            editor.putString("mailid",mailid.getText().toString());
           //For commit changes commit() method is used
            editor.commit();
            Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
        }
        else if(v.getId()==R.id.btnLoad)
        {
        //Load the data
            SharedPreferences sharedPreferences=getSharedPreferences("Data", Context.MODE_PRIVATE);
            String strname=sharedPreferences.getString("name","");
            String strmailid=sharedPreferences.getString("mailid","");
        if (strname.equals("")||strmailid.equals(""))
        {
            Toast.makeText(this, "Data Was Not Found", Toast.LENGTH_SHORT).show();
        }
        else
        {
            name.setText(strname);
            mailid.setText(strmailid);

        }

        }

    }
}


Output

                   

Support us by Sharing