Android Shared Preference Example

In this article i am going to explain about shared preference in android, how to use it and how to add,edit and delete values in android shared preference.

What is shared preference?

Shared preference is one of the way of storing and retrieving primitive data as key value pairs in the android sytem. It can be private or shared. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write.

When to use shared preference?

SharedPreferences can be used whenever you need to save a relatively small collection of key-value pairs.

In this tutorial i will explain how to add,edit and delete shared preference values using the pre defined functions.

Implementation Steps:

In this example i have two edittext fields with id as etName and etMobileNo. And i have four buttons with id as btnSave,btnRetrieve,btnDeleteName and btnDeleteAll. While clicking on btnSave I am saving name and mobileno into the shared preferences using the below code.

After that while clicking on btnRetrieve i am retrieving name and mobileno from shared preference and displaying it in the edittext field using the below code.

[java]btnRetrieve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
etName.setText(sharedPreferences.getString(NAME_KEY, “”));
etMobileNo.setText(sharedPreferences.getString(MOBILE_KEY, “”));
Toast.makeText(MainActivity.this, “Shared preference values retrieved successfully”,
Toast.LENGTH_LONG).show();
}
});[/java]

Now while clicking on btnDeleteName button i am deleting name from the shared preference and refreshing edittext values. Below is the code used.

[java]btnDeleteName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(NAME_KEY).commit();
sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
etName.setText(sharedPreferences.getString(NAME_KEY, “”));
etMobileNo.setText(sharedPreferences.getString(MOBILE_KEY, “”));
Toast.makeText(MainActivity.this, “Name deleted from shared preference”, Toast.LENGTH_LONG).show();
}
});[/java]

Now clicking on btnDeleteAll button i am deleting the shared preference itself and refreshing the edit text values. Below is the entire code used.

MainActivity.Java:

[java]

package com.example.kannadasang.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
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 {
SharedPreferences sharedPreferences;
public static final String SHARED_PREFERENCE_NAME = “com.example.sharedpreference”;
public static final String NAME_KEY = “NAME_KEY”;
public static final String MOBILE_KEY = “MOBILE_KEY”;
EditText etName, etMobileNo;
Button btnSave, btnRetrieve, btnDeleteName, btnDeleteAll;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etMobileNo = (EditText) findViewById(R.id.etMobile);
btnSave = (Button) findViewById(R.id.btnSave);
btnRetrieve = (Button) findViewById(R.id.btnRetrieve);
btnDeleteName = (Button) findViewById(R.id.btnDeleteName);
btnDeleteAll = (Button) findViewById(R.id.btnDeleteAll);

sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etName.getText().toString();
String mobileNo = etMobileNo.getText().toString();

//Add/Update edittext value to shared preference
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(NAME_KEY, name);
editor.putString(MOBILE_KEY, mobileNo);
editor.commit();

//Clearing edittext value
etName.setText(“”);
etMobileNo.setText(“”);
Toast.makeText(MainActivity.this, “Name & mobileno added/updated in shared preference”, Toast.LENGTH_LONG).show();
}
});

btnRetrieve.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
etName.setText(sharedPreferences.getString(NAME_KEY, “”));
etMobileNo.setText(sharedPreferences.getString(MOBILE_KEY, “”));
Toast.makeText(MainActivity.this, “Shared preference values retrieved successfully”, Toast.LENGTH_LONG).show();
}
});

btnDeleteName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(NAME_KEY).commit();
sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
etName.setText(sharedPreferences.getString(NAME_KEY, “”));
etMobileNo.setText(sharedPreferences.getString(MOBILE_KEY, “”));
Toast.makeText(MainActivity.this, “Name deleted from shared preference”, Toast.LENGTH_LONG).show();
}
});

btnDeleteAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear().commit();
sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
etName.setText(sharedPreferences.getString(NAME_KEY, “”));
etMobileNo.setText(sharedPreferences.getString(MOBILE_KEY, “”));
Toast.makeText(MainActivity.this, “Shared preference deleted successfully”, Toast.LENGTH_LONG).show();
}
});
}
}

[/java]

activity_main.xml

[xml]<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/activity_main”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.kannadasang.myapplication.MainActivity”>
<EditText
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:inputType=”text”
android:hint=”Name”
android:ems=”10″
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:id=”@+id/etName” />

<EditText
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:inputType=”number”
android:hint=”Mobile No”
android:ems=”10″
android:layout_below=”@+id/etName”
android:layout_alignRight=”@+id/etName”
android:layout_alignEnd=”@+id/etName”
android:layout_marginTop=”36dp”
android:id=”@+id/etMobile” />

<Button
android:text=”Save/Update”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/etMobile”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”36dp”
android:id=”@+id/btnSave” />

<Button
android:text=”Retrive”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/btnSave”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”25dp”
android:id=”@+id/btnRetrieve” />

<Button
android:text=”Delete Name”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/btnRetrieve”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”17dp”
android:id=”@+id/btnDeleteName” />

<Button
android:text=”Delete All”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/btnDeleteName”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”14dp”
android:id=”@+id/btnDeleteAll” />

</RelativeLayout>[/xml]

Screenshots:

Now if i run the application it will look like below screenshots.

 

Sourcecode:

You can download full sourcecode from the below link.

Leave a Reply

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