Android using of SharedPreferences Tutorial

SharedPreferences

Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve value from saved prefrences.

If you have a relatively small collection of key-values (for example User Name and Password) that you’d like to save, you should use the SharedPreferences APIs.

Step by Step Tutorial

Step 1: Open Android Studio.

Step 2: Create a new project as follows:

If you see the Welcome to Android Studio dialog, choose Start a new Android Studio project, available under ‘Quick Start’ on the right of the dialog.
Otherwise, click File in the Android Studio menu bar, then New, New Project.
Step 3: Enter your app name, company domain, and project location, as prompted. Then click Next.

Step 4: Select the form factors you need for your app. If you’re not sure what you need, just select Phone and Tablet. Then click Next.

Step 5: Select Empty Activity in the ‘Add an activity to Mobile’ dialog. Then click Next.

Step 6: Enter the activity name, layout name and title as prompted. The default values are fine. Then click Finish.

After that wait for few seconds. Android Studio starts Gradle and builds of your project.

Step 7: Now do some changes in your MainActivity.xml file. Add new TextView and EditText control in xml file.

 

Step 8:  Now create object of SharedPreferences and SharedPreferences.Editor

private SharedPreferences sharedPreferences;
private SharedPreferences.Editor sharedPrefEditor;

Step 9: Do following code in onCreate() method.

TextView lblShowPref = (TextView) findViewById(R.id.lblShowSavedPref);
final EditText txtFillPref = (EditText) findViewById(R.id.txtFillPref);
Button btnSave = (Button) findViewById(R.id.btnSave);

sharedPreferences = getSharedPreferences("savedPref",MODE_PRIVATE);
sharedPrefEditor=sharedPreferences.edit();

String SavedPref = sharedPreferences.getString("savedPref", null);
lblShowPref.setText(SavedPref);

btnSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        sharedPrefEditor.putString("savedPref", txtFillPref.getText().toString());
        sharedPrefEditor.commit();
        Toast.makeText(MainActivity.this,"Value Stored in shared prefrences.", Toast.LENGTH_LONG).show();
    }
});

Full Source Code

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.suraj.sharedpreftutorial.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/lblShowSavedPref"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/colorPrimary"
            android:padding="30px"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/txtFillPref"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btnSave"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SAVE" />

    </LinearLayout>


</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.suraj.sharedpreftutorial;

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.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private SharedPreferences sharedPreferences;
    private  SharedPreferences.Editor sharedPrefEditor;

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

        TextView lblShowPref = (TextView) findViewById(R.id.lblShowSavedPref);
        final EditText txtFillPref = (EditText) findViewById(R.id.txtFillPref);
        Button btnSave = (Button) findViewById(R.id.btnSave);

        sharedPreferences = getSharedPreferences("savedPref",MODE_PRIVATE);
        sharedPrefEditor=sharedPreferences.edit();

        String SavedPref = sharedPreferences.getString("savedPref", null);
        lblShowPref.setText(SavedPref);

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                sharedPrefEditor.putString("savedPref", txtFillPref.getText().toString());
                sharedPrefEditor.commit();
                Toast.makeText(MainActivity.this,"Value Stored in shared prefrences.", Toast.LENGTH_LONG).show();
            }
        });
    }
}

 

Post a comment

Your email address will not be published. Required fields are marked *