What is Sharedpreference

There are many ways by which you can store the value.SharedPreference is the way that allow you to store the data in key pair form.The data is persistant if you close the app again open it will display the value as it is.

In my prgram user enter the username and password and click on save button it will save the data in sharedpreference and when you click on the display button it will display the username and password on textview.


SharedPreference:.

In this section i am going to show the sharedpreference with practical example.

First open the android studio and create a project and give the name of your project like SharedPreferenceDemo.

The screenshots of the program is as follows:

sharedpreference image sharedpreference image

Drag and drop the three TextView , one EditText , two Button and one password from pallet. Open the activity_main.xml file and code look like this


<RelativeLayout 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"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="64dp"
        android:layout_marginTop="128dp"
        android:text="UserName" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="92dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="206dp"
        android:text="Password" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:ems="10"
        android:inputType="textPassword"
        android:text="" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/textView"
        android:layout_marginBottom="170dp"
        android:onClick="save"
        android:text="save" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/button"
        android:layout_marginEnd="85dp"
        android:onClick="displayinfo"
        android:text="display" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="139dp"
        android:text="" />


</RelativeLayout>
       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText username,password;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username=(EditText)findViewById(R.id.editText);
        password=(EditText)findViewById(R.id.editText2);
        textView=(TextView)findViewById(R.id.textView3);



    }
    public  void save(View v)
    {
        SharedPreferences sharedPreferences=getSharedPreferences("userinfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();

        editor.putString("username",username.getText().toString());
        editor.putString("password",password.getText().toString());

        editor.apply();
        Toast.makeText(MainActivity.this,"Saved",Toast.LENGTH_LONG).show();

    }

    public  void displayinfo(View v)
    {
        SharedPreferences sharedPreferences=getSharedPreferences("userinfo",MODE_PRIVATE);

       String user =sharedPreferences.getString("username","");
       String pwd=sharedPreferences.getString("password","");
       textView.setText(user+" "+pwd);

    }

}



Explanation:

EditText username,password:This statement declare the object username,password of EditText.

TextView textView:This statement declare the object textView of TextView.

username=(EditText)findViewById(R.id.editText):This statement initialized the username object.

password=(EditText)findViewById(R.id.editText2);:This statement initialized the password object.

textView=(TextView)findViewById(R.id.textView3):This statement initialized the textView object.

public void save(View v):Here i have created the method save for saving the data that is coming form username,password object.

SharedPreferences sharedPreferences=getSharedPreferences("userinfo",MODE_PRIVATE):
The getSharedPreference() return the SharedPreference object and it will take the two argument first is name and second is mode.

SharedPreferences.Editor editor=sharedPreferences.edit():the edit() return the editor object that will be used to call the method putString().

editor.putString("username",username.getText().toString()):The putString() is used the store the value into preferences. It will take two argument first is key and second is value.In my case username is key and username.getText() provide the value.

editor.putString("password",password.getText().toString()):The putString() is used the store the value into preferences. It will take two argument first is key and second is value.In my case password is key and password.getText() provide the value.

editor.apply():Set a set of String values in the preferences editor.

Toast.makeText(MainActivity.this,"Saved",Toast.LENGTH_LONG).show():The Toast display the message save.

the save() we need to set the android:onClick property of save button.

public void displayinfo(View v)I have created the displayinfo() that will display the username and password on textView.

SharedPreferences sharedPreferences=getSharedPreferences("userinfo",MODE_PRIVATE):
The getSharedPreference() return the SharedPreference object and it will take the two argument first is name and second is mode.

String user =sharedPreferences.getString("username",""):The getString() retrieve the data from preference.It will retrieve the username from preference and assign into user variable.

String pwd=sharedPreferences.getString("password",""):The getString() retrieve the data from preference.It will retrieve the password from preference and assign into pwd variable.

textView.setText(user+" "+pwd):This statement set the user and pwd into textView.


SharedPreference

what is SharedPreference for video click here

back image                     next image