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. <LinearLayout
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:id="@+id/linearlayout1"
  14. android:orientation="horizontal">
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="Store value"
  19. android:id="@+id/textViewNaame"/>
  20. <EditText
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:id="@+id/name"
  24. android:layout_marginLeft="50dp"/>
  25. </LinearLayout>
  26. <Button
  27. android:layout_width="100dp"
  28. android:layout_height="wrap_content"
  29. android:text="stored"
  30. android:id="@+id/button" android:layout_below="@+id/textViewretrive" android:layout_centerHorizontal="true"
  31. android:layout_marginTop="39dp"/>
  32. <TextView
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="retrievedata"
  36. android:id="@+id/textViewretrive"
  37. android:layout_marginTop="26dp" android:layout_below="@+id/linearlayout1"
  38. android:layout_alignLeft="@+id/linearlayout1"/>
  39. <Button
  40. android:layout_width="100dp"
  41. android:layout_height="wrap_content"
  42. android:text="retrieve"
  43. android:id="@+id/button2" android:layout_below="@+id/button" android:layout_alignLeft="@+id/button"
  44. android:layout_marginTop="42dp"/>
  45. </RelativeLayout>
Step 3
  1. package com.sharedprefrence;
  2. import android.content.SharedPreferences;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11. public class MainActivity extends Activity {
  12. EditText edttextname,edttextword;
  13. TextView txtviewname,txtviewword,txtviewretrieve;
  14. SharedPreferences settings;
  15. Button btnstore,btnretrieve;
  16. public static final String PREF="any_pref";
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. edttextname=(EditText)findViewById(R.id.name);
  22. txtviewname=(TextView)findViewById(R.id.textViewNaame);
  23. btnstore=(Button)findViewById(R.id.button);
  24. txtviewretrieve=(TextView)findViewById(R.id.textViewretrive);
  25. btnretrieve=(Button)findViewById(R.id.button2);
  26. btnstore.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View view) {
  29. String name=edttextname.getText().toString();
  30. SharedPreferences store = getSharedPreferences(PREF, MODE_PRIVATE);
  31. SharedPreferences.Editor editor = store.edit();
  32. editor.putString("any_pref",name);
  33. editor.commit();
  34. Toast.makeText(getApplicationContext(),
  35. "Store", Toast.LENGTH_LONG).show();
  36. }
  37. });
  38. btnretrieve.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View view) {
  41. SharedPreferences store = getSharedPreferences(PREF, MODE_PRIVATE);
  42. String retrievestring = (store.getString("any_pref", ""));
  43. txtviewretrieve.setText(retrievestring);
  44. Toast.makeText(getApplicationContext(),
  45. "Retrive", Toast.LENGTH_LONG).show();
  46. }
  47. });
  48. }
  49. }
Step 4
See the output:
image store.jpg
Step 5
image retrieve.jpg