Shared Preferences is one of the ways provided by Android to store data for e.g. when you want to store user/app data globally through out the application. Shared preferences stores data even though user closes the application.
Shared Preferences gives the facility to save and retrieve data in <key,value> pair.
Following example is an illustration of storing different types of data like (String, Int, float, boolean) in the SharedPreferences. To achieve this just follow the 2 simple steps

Step 1. Create a xml named acitvity_main.xml
Capture
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”
android:background=”#9a9a9a9a”>
<EditText
android:id=”@+id/textTitle”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:background=”#207099″
android:gravity=”center”
android:padding=”40dp”
android:text=”Employee Details”
android:textColor=”#ffffff”
android:textSize=”30sp” />

<TextView
android:id=”@+id/lbl_name”
android:layout_width=”80dp”
android:layout_height=”wrap_content”
android:layout_alignParentStart=”true”
android:layout_alignParentLeft=”true”
android:layout_alignTop=”@+id/textName”
android:layout_marginLeft=”10dp”
android:gravity=”fill”
android:text=”Name”
android:textColor=”#207099″
android:textSize=”25sp” />

<EditText
android:id=”@+id/textName”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textTitle”
android:layout_marginLeft=”10dp”
android:layout_marginTop=”30dp”
android:layout_toRightOf=”@+id/lbl_name”
android:layout_marginRight=”10dp”
android:padding=”5dp”
android:textSize=”25sp” />

<TextView
android:id=”@+id/lbl_age”
android:layout_width=”80dp”
android:layout_height=”wrap_content”
android:layout_alignParentStart=”true”
android:layout_alignParentLeft=”true”
android:layout_alignTop=”@+id/textAge”
android:layout_marginLeft=”10dp”
android:gravity=”fill”
android:text=”Age”
android:textColor=”#207099″
android:textSize=”25sp” />

<EditText
android:id=”@+id/textAge”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textName”
android:layout_marginLeft=”10dp”
android:layout_marginTop=”10dp”
android:layout_marginRight=”10dp”
android:layout_toRightOf=”@+id/lbl_age”
android:padding=”5dp”
android:textSize=”25sp” />

<TextView
android:id=”@+id/lbl_salary”
android:layout_width=”80dp”
android:layout_height=”wrap_content”
android:layout_alignParentStart=”true”
android:layout_alignParentLeft=”true”
android:layout_alignTop=”@+id/textSalary”
android:layout_marginLeft=”10dp”
android:gravity=”fill”
android:text=”Salary”
android:textColor=”#207099″
android:textSize=”25sp” />
<EditText
android:id=”@+id/textSalary”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textAge”
android:layout_marginLeft=”10dp”
android:layout_marginTop=”10dp”
android:layout_marginRight=”10dp”
android:layout_toRightOf=”@+id/lbl_salary”
android:padding=”5dp”
android:textSize=”24sp” />

<TextView
android:id=”@+id/lbl_emptype”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentStart=”true”
android:layout_alignParentLeft=”true”
android:layout_alignTop=”@+id/textEmpType”
android:layout_marginLeft=”10dp”
android:gravity=”fill”
android:text=”Full Time Employee”
android:textColor=”#207099″
android:textSize=”25sp” />
<EditText
android:id=”@+id/textEmpType”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_below=”@+id/textSalary”
android:layout_marginLeft=”7dp”
android:layout_marginTop=”10dp”
android:layout_toRightOf=”@+id/lbl_emptype”
android:hint=” true/false”
android:padding=”5dp”
android:layout_marginRight=”10dp”
android:textColor=”#262626″
android:textSize=”24sp” />
<Button
android:id=”@+id/btnShow”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”37dp”
android:text=”Show Data”
android:layout_below=”@+id/textEmpType”
android:layout_alignStart=”@+id/textEmpType”
android:layout_alignLeft=”@+id/textEmpType”/>

<Button
android:id=”@+id/btnSave”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Save Data”
android:layout_alignTop=”@+id/btnShow”
android:layout_alignStart=”@+id/lbl_emptype”
android:layout_alignLeft=”@+id/lbl_emptype”
android:layout_marginStart=”40dp”
android:layout_marginLeft=”40dp”/>
</RelativeLayout>

Step 2 : Create an activity named MainActivity.java
package test.example.com.sharedprefrencesexample;

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.Toast;

public class MainActivity extends AppCompatActivity {

SharedPreferences sharePref;
SharedPreferences.Editor editor;

EditText textName;
EditText textAge;
EditText textSalary;
EditText textEmpType;

Button btnSave;
Button btnShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sharePref = getApplicationContext().getSharedPreferences(“EmployeeDetails”, MODE_PRIVATE);
editor = sharePref.edit();

textName = (EditText) findViewById(R.id.textName);
textAge = (EditText) findViewById(R.id.textAge);
textSalary = (EditText) findViewById(R.id.textSalary);
textEmpType = (EditText) findViewById(R.id.textEmpType);

btnSave = (Button) findViewById(R.id.btnSave);
btnShow = (Button) findViewById(R.id.btnShow);

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Inserting data to sharedPreferences
editor.putString(“Emp_Name”, textName.getText().toString());
editor.putInt(“Emp_Age”, Integer.parseInt(textAge.getText().toString()));
editor.putFloat(“Emp_Salary”, Float.parseFloat(textSalary.getText().toString()));
editor.putBoolean(“Emp_Type”, Boolean.parseBoolean(textEmpType.getText().toString()));

editor.commit();

textName.setText(“”);
textAge.setText(“”);
textSalary.setText(“”);
textEmpType.setText(“”);

}
});

// Showing the stored SharedPreferences data in a Toast
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//sharePref.getString(“Emp_Name”,null);
Toast.makeText(getApplicationContext(), “Name: ” + sharePref.getString(“Emp_Name”, null), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), “Age is: ” + sharePref.getInt(“Emp_Age”, 0) + “”, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), “Salary is: ” + sharePref.getFloat(“Emp_Salary”, 0f) + “”, Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), “Is he/she a Permanent Employee: ” + sharePref.getBoolean(“Emp_Type”, false) + “”, Toast.LENGTH_SHORT).show();
}
});

}

}

And you are done, now just run the project and enter the information asked

Advertisements