Shared Preferences Example In Android

Shared Preferences In Android

Shared Preferences in Android is used to store or save small amount of data into an Android Application in the form of key and value pair. 

Example:
In this tutorial we will learn to save data in to Android Shared Preferences with an example.
  • Create a new project Sharedpreference in Android Studio.
  • Design the main layout as follows.
File: activity_main.xml:

<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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hss_24.sharedpreference.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:gravity="center"
        android:text="Android Lovers"
        android:textColor="@color/colorPrimary"
        android:textSize="28sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:gravity="center"
        android:text="Shared Preferences"
        android:textColor="@color/colorPrimary"
        android:textSize="28sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/email_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:hint="Enter email" />

    <EditText
        android:id="@+id/password_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter password" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:gravity="center">

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="Save" />

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="Show" />

    </LinearLayout>

</LinearLayout>

  • Modify the main java file as follows.
File: MainActivity.java:

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Button login = (Button) findViewById(R.id.login);
        Button save = (Button) findViewById(R.id.save);

        final EditText email = (EditText) findViewById(R.id.email_edit);
        final EditText password = (EditText) findViewById(R.id.password_edit);

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email_str = email.getText().toString();
                String pass_str = password.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences("preferences", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("email", email_str);
                editor.putString("password", pass_str);
                editor.commit();
                Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("preferences", Context.MODE_PRIVATE);
                String str = null;
                String str2 = null;
                if (sharedPreferences.contains("email")) {
                    str = sharedPreferences.getString("email", "");

                }

                if (sharedPreferences.contains("password")) {
                    str2 = sharedPreferences.getString("password", "");

                }

                Toast.makeText(getApplicationContext(), "" + str + "\n" + "" + str2, Toast.LENGTH_LONG).show();
            }
        });
    }
}

  • Here we are storing the editText data into string and saving the string in shared preferences on click of save button and showing that data in Toast on click of show button.
  • Run the App and enjoy the output.
Output:



Download project



Thank You...


Comments