May 07, 2014

SharedPreferences In Android.


                  SharedPreferences is one of the storage mechanism in Android. The value we are storing in Shared Preferences is permanent so the value stored will remains there when we close the application. Here the storing of data is done as key value pair. Here we store the data with the help of an editor.  To get the value we have saved we need to call getSharedPreferences() method.

Here I am going to Explain How to save the details Id, Name, Designation, Phone number of an Employee in  SharedPreferences.

Here Am going to create a class EmployeeSession to store the details,

public class EmployeeSession {

      private static final String  SharedPreference_Employee = "Employee_Preferences";
     // Employee_Preferences is the Name of the SharedPreferences.

    private static final String Emp_Id = "EmployeeId";
    private static final String Emp_Name = "EmployeeName";
    private static final String Emp_Designation = "EmployeeDesignation";
    private static final String Emp_Phone = "EmployeePhone";

    private SharedPreferences sharedPref;
    private Editor editor;

    public EmployeeSession(Context context) {
        sharedPref = context.getSharedPreferences(SharedPreference_Employee, Context.MODE_PRIVATE);
        editor = sharedPref.edit();

    public void ResetEmployee() {
       // Function To clear all the Employee Details

   public void setEmployeeId(Int Id) {
      // Function To save Employee Id
        editor.putInt(Emp_Id, Id);

    public Int getEmployeeId() {
       // Function To get Employee Id
        return sharedPref.getInt(Emp_Id, 0); 

    public void setEmployeeName(String Name) {
       // Function To save Employee Name
        editor.putString(Emp_Name, Name);

    public String getEmployeeName() {
       // Function To get Employee Name
        return sharedPref.getString(Emp_Name, null);

    public void setEmployeeDesignation(String Designation) {
        // Function To save Employee Designation
        editor.putString(Emp_Designation, Designation);

    public String getEmployeeDesignation() {
        // Function To get Employee Designation
        return sharedPref.getString(Emp_Designation, null);

   public void setEmployeePhone(String Phone) {
       // Function To save Employee Phone Number
        editor.putString(Emp_Phone, Phone);

    public String getEmployeePhone() {
        // Function To get Employee Phone Number
        return sharedPref.getString(Emp_Phone, null);


       In the Above example I am saving only Int and String values. You can Also save Int, String, Boolean, Float, Long values in SharedPreferences. Here is am example of saving and retrieve of values.

Save / Update Valus

  editor.putInt(IntKeyValue, Value);   // Here Value is the Int value to store
  editor.putString(StringKeyValue, Value);  // Here Value is the String value to store
  editor.putBoolean(BooleanKeyValue, Value);   // Here Value is the Booleanvalue to store
  editor.putFloat(FloatKeyValue, Value);  // Here Value is the Float value to store
  editor.putLong(LongKeyValue, Value);    // Here Value is the Long value to store

 editor.commit();    // This method is used to save the changes.

Retrieve Values

  sharedPref.getInt(IntKeyValue, Value);   // Here Value is the default value to return if it is Null
  sharedPref.getString(StringKeyValue, Value);  // Here Value is the default value to return if it is Null
  sharedPref.getBoolean(BooleanKeyValue, Value);  // Here Value is the default value to return if it is Null
  sharedPref.getFloat(FloatKeyValue, Value);  // Here Value is the default value to return if it is Null
  sharedPref.getLong(LongKeyValue, Value);    //  Here Value is the default value to return if it is Null

Delete/ Clear

To delete a particular key you can do like,

editor.remove("KeyName1");   // To delete the Key KeyName1
editor.remove("KeyName2");   // To delete the Key KeyName2
editor.commit();  // To save the changes.

     To clear All the Values in SharedPreferences we can use clear() function. Here is the Example,

editor.commit(); // To save the changes.

No comments:

Post a Comment