Skip to main content

SharedPreferences In Android.


SharedPreferences

                  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
        editor.clear();
        editor.commit();
    }

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

    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);
        editor.commit();
    }

    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);
        editor.commit();
    }

    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);
        editor.commit();
    }

    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.clear();
editor.commit(); // To save the changes.


Popular posts from this blog

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_con…

How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.

In this blog I want to explain How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.


Intent
                Intent is an object that is used to launch an activity or to return information  from an activity.  To start a new activity using startActivity(intent).  To get information from another activity we start activity using startActivityForResult(intent, requestCode).


               Here in  FromPage.java we are adding  Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List values to an intent. In ToPage.java We extract added values will retrieve from intent. 


FromPage.java

ArrayList<Integer>  IntArrayList = new  ArrayList<Integer>(Arrays.asList(100,13,18,22,44,13));
ArrayList<String>  StringArrayList = new  ArrayList<String>(Arrays.asList("String1", "String2", "String3", "String4"));

        int[] intList = {4, 6, 3, …

Importing Image From Gallery

In this tutorial I want to explain how to import images from Gallery to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will redirect to Gallery to Browse image. While we select an image it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        …