Android shared preference example with easy explanation

In this tutorials we will learn about the android “shared preference” which is used to save and retrieve the data in the form of key-value pair.

Introduction

Shared preference provides the facility in which we can save and retrieve the value/data in the form of key-value pair.

Shared preference only store the primitive data types such integer,float,string,double etc.

Shared preference has lost the data on performing the two operations

1.Uninstalling the application

2.Clearing the application data(go to setting)

To use the shared preference we need to call the getSharedPreferences() method that returns the value of preference.which is hold by shared preference file.

SharedPreference sharedpreference=getSharedPreferences(myPreference,mode);

Here “myPreference” is the file name where the data is stored and “mode” means simply accessibility of data.

Some modes are given below:

1.MODE_PRIVATE-Mode private means shared preference data can be used within the application only.we can not use the data outside the application.

2.MODE_WORLD_READABLE-Shared preference data can be used outside the application means world wide.This type of mode is not used by the developer.

3.MODE_APPEND-This mode mode will append the new preference with the already existing preference.

Initialization

We need an Editor to edit and changes into the shared preference.The code is given below to get the shared preference.

SharedPreference pref=getApplicationContext("mypreference",0)//0 for the private mode
Editor editor=pref.edit();

Saving Data

Saving/storing data in shared preference we use the editor.commit() or editor.apply().

editor.putString("key_name","suman");//storing the string value
editor.putBoolean("key_name",true);//storing the boolean value true or false
editor.commit();//commit is used to save the changes

Retrieving Data

To retrieve the data from the shared preference we use the getString,getBoolean,getInt etc according to which type of data we have saved into the shared preference.

pref.getString("key_name,null);//Here we will get the string "suman".
pref.getBolean("key_name,null);//Here we will get the boolean value true.

Remove or Clear Data

Using the remove(“key_name”) .we can remove the particular data.

using ‘clear’ we can remove or delete all the shared preference data.

editor.remove("key_name")//data will be remove on the specified key. 
editor.commit();

To clear the whole data.
editor.clear();
editor.commit();

Shared preference example

Here we will understand about the shared preference with the help of example step by step:


activity_main.xml

The activity_main.xml layout consist the two EditText Views where we will enter the name and email and one button.Here button will be used for save the data on the onClick of button.

<?xml version="1.0" encoding="utf-8" ?>
<RlativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="mathch_parent"
android:layout_height="match_parent"
android:Orientation="vertical"/>

<LinearLayout
android:layout_width="mathch_parent"
android:layout_height="wrap_content"
android:Orientation="vertical"/>

<EditText
android:id="@+id/name"
android:layout_width="mathch_parent"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/email"
android:layout_width="mathch_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/save_data_button"
android:layout_width="mathch_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

</RelativeLayout>

MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity
{
private SharedPreferences sharedpreferences;
private TextView name;
private TextView email;
private Button saveData;
public static final String myPreference="mypref";
public static final String Name="name";
public static final String Email="email";

@Override
protected void  onCreate(Bundle SavedInstanceState)
{
super.onCreate(SavedInstanceBundle)
setContentView(R.layout.activity_main);

name=(EditText)findViewById(R.id.name)
emial=(EditText)findViewById(R.id.email);
saveData=(Button)findViewById(R.id.save_data_button);
saveData.setOnclickListener(listener);

}  

//Here we are saving the data into the shared preference on the button click.
private View.onClickListener listener=new View.OnClickListener
{
@Override
public void onClick()
{
SharedPreference.Editor editor=sharedpreferences.edit();
editor.putString(Name,name);
editor.putString(Email,email);
editor.apply();
}        
};
}


sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *