Introduction
In this article, we will learn how to develop “remember me check” with functionality in Android.
- In Android usually, we need to persist user session credentials in our application where user session credentials contain the username, email, password and so on.
- Using SharedPreferences we can store application preferences data persistently.
- In other words, data won't be lost until the application is uninstalled because the preferences data can be stored as key/value pairs and are available across all the Activities of the application or to a specific Activity.
- Application preferences data will be available even when the device starts or stops (the user closes the app and re-open it).
- SharedPreferences is used to store primitive data-types
Boolean, float, int, long, string
SharedPreferences can be used to store data across all the applications or to the specific activity.
- getSharedPreferences()- Preferences persisted across all over the application by the name passed to it as a first argument.
- getPreferences()- Preferences persisted for a specific Activity so there is no need to specify the name since there will be only one file for the activity.
For instance-
- private SharedPreferences savePreferences;
- savePreferences = getSharedPreferences("savePrefs", 0);
Instead of 0 we can use
Context.MODE_PRIVATE.
We can store as many preferences as we want for our application and all of them will continue to be saved in the
savePrefs.xml file because we gave the name “savePrefs” in getSharedPreferences.
The
savePrefs.xml file is usually located at,
- /data/data/[package name]/shared_prefs/savePrefs.xml
Also we can browse it in the File Explorer in the DDMS view.
For instance,
1. Setting or Updating Preferences
To set primitive data, Sharedpreferences has some methods, for instance,
- putBoolean(), putInt(), putFloat(), putString() and so on.
Method
- private SharedPreferences savePreferences;
- private SharedPreferences.Editor savePrefsEditor;
- savePreferences = getSharedPreferences("savePrefs", 0);
- savePrefsEditor = savePreferences.edit();
-
-
- if (saveDetailsCheck.isChecked()) {
-
- savePrefsEditor.putBoolean("saveLogin", true);
- savePrefsEditor.putString("username", username);
- savePrefsEditor.putString("password", email);
-
- savePrefsEditor.commit();
- Toast.makeText(getApplicationContext(), "Your details have been saved.", Toast.LENGTH_LONG).show();
- }
2. Fetching Preferences
For fetching purposes, the object of SharedPreferences is directly used. There is no need for SharedPreferences.Editor.
There are the following methods,
- getBoolean(), getFloat(), getInt(), getLong(), getString()
Method
- private Boolean saveLogin;
- saveLogin = savePreferences.getBoolean("saveLogin", false);
- if (saveLogin == true)
- {
- UserName.setText(savePreferences.getString("username", ""));
- Email.setText(savePreferences.getString("password", ""));
- saveDetailsCheck.setChecked(true);
- }
Note- Get methods to have two arguments where the first is the name of the key whereas the second one is the default value to return if the preference does not exist.
3. Clear Preferences
-
- savePrefsEditor.clear();
- savePrefsEditor.commit();
Source Code
- MainActivity.java
-
- package com.example.loginpreferencesdemo;
-
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
- EditText UserName, Email;
- Button Save;
- String username;
- String email;
-
- private CheckBox saveDetailsCheck;
- private SharedPreferences savePreferences;
- private SharedPreferences.Editor savePrefsEditor;
- private Boolean saveLogin;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.fragment_main);
-
- UserName = (EditText) findViewById(R.id.editText1);
- Email = (EditText) findViewById(R.id.editText2);
- Save = (Button) findViewById(R.id.button1);
-
- saveDetailsCheck = (CheckBox) findViewById(R.id.checkBox1);
-
- savePreferences = getSharedPreferences("savePrefs", 0);
- savePrefsEditor = savePreferences.edit();
-
- saveLogin = savePreferences.getBoolean("saveLogin", false);
- if (saveLogin == true)
-
- {
- UserName.setText(savePreferences.getString("username", ""));
- Email.setText(savePreferences.getString("password", ""));
- saveDetailsCheck.setChecked(true);
- }
-
- Save.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- username = UserName.getText().toString();
- email = Email.getText().toString();
-
- if (saveDetailsCheck.isChecked()) {
- savePrefsEditor.putBoolean("saveLogin", true);
- savePrefsEditor.putString("username", username);
- savePrefsEditor.putString("password", email);
- savePrefsEditor.commit();
- Toast.makeText(getApplicationContext(), "Your details have been saved.", Toast.LENGTH_LONG).show();
-
- } else {
-
- savePrefsEditor.clear();
- savePrefsEditor.commit();
-
- }
- }
- });
- }
-
- }
fragment_main.xml
- <RelativeLayout
- 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: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="com.example.loginpreferencesdemo.MainActivity$PlaceholderFragment" >
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Shared Preferences Demo By Abhijeet" />
- <EditText
- android:id="@+id/editText1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/textView1"
- android:layout_below="@+id/textView1"
- android:layout_marginTop="60dp"
- android:ems="10"
- android:hint="UserName" ></EditText>
- <EditText
- android:id="@+id/editText2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/editText1"
- android:layout_below="@+id/editText1"
- android:layout_marginTop="26dp"
- android:ems="10"
- android:hint="Email ID" />
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/editText2"
- android:layout_below="@+id/editText2"
- android:layout_marginTop="34dp"
- android:text="Save Details" />
- <CheckBox
- android:id="@+id/checkBox1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignRight="@+id/editText2"
- android:layout_below="@+id/editText2"
- android:text="Remember Me" />
- </RelativeLayout>
Output
Before doing that remember to check:
After remember to check: