Shared Preference in Android Example.

hello friends,
today we are going to learn about shared preference in android.
In Android, we can able to store data in multiple ways.
one of that it's called shared preference.
into the shared preference we can store data for some time.
Let's take an example.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/editfirst"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Enter FirstName"
        android:inputType="text" />
    <EditText
        android:id="@+id/editlast"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="Enter LastName"
        android:inputType="text" />
    <Button
        android:id="@+id/btnsave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="saveData"
        android:text="Save" />
    <Button
        android:id="@+id/btnretr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="getData"
        android:text="Retrieve" />
</LinearLayout>


MainActivity.java

public class MainActivity extends AppCompatActivity {

    SharedPreferences sharedpreferences;
    EditText fname;
    EditText lname;
    public static final String mypreference = "mypref";
    public static final String Name = "firstname";
    public static final String Email = "lastname";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fname = (EditText) findViewById(R.id.editfirst);
        lname = (EditText) findViewById(R.id.editlast);

        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        if (sharedpreferences.contains(Name)) {
            fname.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Email)) {
            lname.setText(sharedpreferences.getString(Email, ""));

        }
    }
    public void saveData(View view) {
        String n = fname.getText().toString();
        String e = lname.getText().toString();
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.putString(Name, n);
        editor.putString(Email, e);
        editor.commit();
        Toast.makeText(getApplicationContext(), "Data Saved.", Toast.LENGTH_SHORT).show();
        fname.setText("");
        lname.setText("");
    }

    public void getData(View view) {
        sharedpreferences = getSharedPreferences(mypreference,
                Context.MODE_PRIVATE);
        if (sharedpreferences.contains(Name)) {
            fname.setText(sharedpreferences.getString(Name, ""));
        }
        if (sharedpreferences.contains(Email)) {
            lname.setText(sharedpreferences.getString(Email, ""));
        }
    }
}



OUTPUT :- 



Comments