Shared Preference in Android

Introduction

 
This article explains what a Shared Preference in Android is. A Shared Preference stores a value and retrieves a key, value pair of data.
 
Before proceeding to the tutorial I will explain the basics necessary for working with Shared Preferences.
 
We obtain an object of the Shared Preference class by calling the getSharedPrefrence(string name, int mode) method that takes a string value and an integer type value as parameters.
  1. SharedPreferences pref = getSharedPreferences("MyPref"0);  
For private mode, we can use the value 0.
 
After that, we get an instance of the SharedPrefrence.Editor by which we call the edit() method to store the value that we want.
  1. Editor editor = pref.edit(); 
Finally we call the commit method with the editor, as in:
  1. editor.commit (); 
We can store any primitive data type (int, float, long string, boolean ) using sharedpreference by calling the method with an editor object, as in: 
  1. editor.putBoolean("key_name"true); // Storing boolean - true/false  
  2. editor.putString("key_name""string value"); // Storing string  
  3. editor.putInt("key_name""int value"); // Storing integer  
  4. editor.putFloat("key_name""float value"); // Storing float  
  5. editor.putLong("key_name""long value"); // Storing long  
  6. editor.commit(); 
Step 1
 
Create a new project as "File" -> "New" -> "Android Application project" as shown below.
 
Shared.jpg
 
Step 2
 
Now open the XML file  "res/layout/activity_main.xml" and update it as in the following code.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <LinearLayout  
  12.             android:layout_width="fill_parent"  
  13.             android:layout_height="wrap_content"  
  14.             android:id="@+id/linearlayout1"  
  15.           android:orientation="horizontal">  
  16.    
  17.         <TextView  
  18.                 android:layout_width="wrap_content"  
  19.                 android:layout_height="wrap_content"  
  20.                 android:text="Store value"  
  21.                 android:id="@+id/textViewNaame"/>  
  22.         <EditText  
  23.                 android:layout_width="fill_parent"  
  24.                 android:layout_height="wrap_content"  
  25.                 android:id="@+id/name"  
  26.                 android:layout_marginLeft="50dp"/>  
  27.    
  28.     </LinearLayout>  
  29.    
  30.     <Button  
  31.             android:layout_width="100dp"  
  32.             android:layout_height="wrap_content"  
  33.             android:text="stored"  
  34.             android:id="@+id/button" android:layout_below="@+id/textViewretrive" android:layout_centerHorizontal="true"  
  35.             android:layout_marginTop="39dp"/>  
  36.     <TextView  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:text="retrievedata"  
  40.             android:id="@+id/textViewretrive"  
  41.    
  42.             android:layout_marginTop="26dp" android:layout_below="@+id/linearlayout1"  
  43.             android:layout_alignLeft="@+id/linearlayout1"/>  
  44.     <Button  
  45.             android:layout_width="100dp"  
  46.             android:layout_height="wrap_content"  
  47.             android:text="retrieve"  
  48.             android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignLeft="@+id/button"  
  49.             android:layout_marginTop="42dp"/>  
  50. </RelativeLayout> 
Step 3
  1. package com.sharedprefrence;  
  2.    
  3. import android.content.SharedPreferences;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10. import android.widget.Button;  
  11. import android.widget.Toast;  
  12.    
  13. public class MainActivity extends Activity {  
  14.    
  15.     EditText edttextname,edttextword;  
  16.     TextView txtviewname,txtviewword,txtviewretrieve;  
  17.     SharedPreferences settings;  
  18.     Button btnstore,btnretrieve;  
  19.     public static final String PREF="any_pref";  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.    
  25.         edttextname=(EditText)findViewById(R.id.name);  
  26.         txtviewname=(TextView)findViewById(R.id.textViewNaame);  
  27.         btnstore=(Button)findViewById(R.id.button);  
  28.         txtviewretrieve=(TextView)findViewById(R.id.textViewretrive);  
  29.         btnretrieve=(Button)findViewById(R.id.button2);  
  30.    
  31.     btnstore.setOnClickListener(new View.OnClickListener() {  
  32.         @Override  
  33.         public void onClick(View view) {  
  34.    
  35.             String name=edttextname.getText().toString();  
  36.             SharedPreferences store = getSharedPreferences(PREF, MODE_PRIVATE);  
  37.             SharedPreferences.Editor editor = store.edit();  
  38.             editor.putString("any_pref",name);  
  39.             editor.commit();  
  40.             Toast.makeText(getApplicationContext(),  
  41.                     "Store", Toast.LENGTH_LONG).show();  
  42.     }  
  43.     });  
  44.     btnretrieve.setOnClickListener(new View.OnClickListener() {  
  45.         @Override  
  46.         public void onClick(View view) {  
  47.             SharedPreferences store = getSharedPreferences(PREF, MODE_PRIVATE);  
  48.             String retrievestring = (store.getString("any_pref"""));  
  49.             txtviewretrieve.setText(retrievestring);  
  50.             Toast.makeText(getApplicationContext(),  
  51.                     "Retrive", Toast.LENGTH_LONG).show();  
  52.         }  
  53.     });  
  54. }  

Step 4
 
See the output:
 
image store.jpg
 
Step 5
 
image retrieve.jpg


Similar Articles