Add a "Remember Me" Checkbox

Add a Remember me checkbox

I just built this into my app, here's the basic code and some explanation:

Basically the key here is SharedPreferences. You'll setup a SharedPreferences object and store your username and password after the user has entered them. Then, when they run the application again, the preferences will have their data stored and will then repopulate the login fields.

LoginActivity.java

package com.realsimpleapps.LoginTesting;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class LoginActivity extends Activity implements OnClickListener {

private String username,password;
private Button ok;
private EditText editTextUsername,editTextPassword;
private CheckBox saveLoginCheckBox;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

ok = (Button)findViewById(R.id.buttonLogin);
ok.setOnClickListener(this);
editTextUsername = (EditText)findViewById(R.id.editTextUsername);
editTextPassword = (EditText)findViewById(R.id.editTextPassword);
saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();

saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
editTextUsername.setText(loginPreferences.getString("username", ""));
editTextPassword.setText(loginPreferences.getString("password", ""));
saveLoginCheckBox.setChecked(true);
}
}

public void onClick(View view) {
if (view == ok) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextUsername.getWindowToken(), 0);

username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();

if (saveLoginCheckBox.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", username);
loginPrefsEditor.putString("password", password);
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}

doSomethingElse();
}
}

public void doSomethingElse() {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
LoginActivity.this.finish();
}
}

The method at end, doSomethingElse() is your placeholder to go to the next step for your application. My doSomethingElse() method simply loads another activity.

Here's a basic xml file for the login page:

login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:padding="10dp" >

<EditText
android:id="@+id/editTextUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:hint="Username"
android:inputType="textNoSuggestions"
android:imeOptions="actionNext" />

<EditText
android:id="@+id/editTextPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editTextUsername"
android:hint="Password"
android:inputType="textPassword"
android:imeOptions="actionDone" />

<CheckBox
android:id="@+id/saveLoginCheckBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editTextPassword"
android:text="Save Login?"
android:textColor="#FFF" />

<Button
android:id="@+id/buttonLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/saveLoginCheckBox"
android:layout_marginTop="40dp"
android:text="Login" />

</RelativeLayout>

IMPORTANT: You'll likely want to encrypt the password before storing it in SharedPreferences. Details for that are beyond the scope of this question, but here is the code I used to do that: http://www.androidsnippets.com/encryptdecrypt-strings. You'll have to come up with some kind of key schema too.

This code has been tested on Android 2.1, SDK 7. Let me know how it works for you.

David

Add Remember me checkbox to the login page

Need to override AuthService and add internal variable and then read it.

How to Add 'Remember Me' Checkbox

If you want the app to remember your credentials after closing the app and reopening you can use SharedPreferences.

I did this the other day and this was my solution...

For saving the credentials you do this

    private void savePreferences() {
SharedPreferences settings = getSharedPreferences("Login",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();

if (remLoginCredentials = true) {
editor.putString("Uname", email.getText().toString());
editor.putString("Pass", password.getText().toString());
editor.putString("Rem", String.valueOf(rememberLogin.isChecked()));
editor.apply();
} else {
editor.putString("Rem", String.valueOf(rememberLogin.isChecked()));
editor.apply();
}
}

And to load the credentials you can do this.

    private void loadPreferences() {
SharedPreferences settings = getSharedPreferences("Login",
Context.MODE_PRIVATE);

if (settings.getString("Rem", null).equals("true")) {
email.setText(settings.getString("Uname", null));
password.setText(settings.getString("Pass", null));
rememberLogin.setChecked(true);
remLoginCredentials = true;
} else {
email.setText("");
password.setText("");
rememberLogin.setChecked(false);
remLoginCredentials = false;
}
}

How to introduce 'Remember me' checkbox to existing project with Spring Security?

I think the below links will be very useful,

  • Remember-Me Authentication
  • Configuring Spring Security Form Login with Remember-Me Enabled
  • 5 Minute Guide to Spring Security

remember me checkbox in login using cookie in codeignitor

firstly you have to include cookie helper as I mention in the comment section

After that in your controller

public function loginaction()
{
$this->load->helper('cookie');
$email=$this->input->post('email');
$password=$this->input->post('password');
$where = array('email'=>$email,'password'=>$password);
$tbname='login';
$query = $this->Insert_Model->viewdata($tbname,$where);

if(empty($query))
{
$data['msg']="Invalid email or password";
$this->load->view('login',$data);
}
else
{
//first you have to delete old cookie and create new one
delete_cookie("email");
delete_cookie("password");
if ($this->input->post('remember') == 'true') {

$userName = array(
'name' => 'email',
'value' => YOUREMAIL,
'expire' => '86500',
'prefix' => '',
'secure' => FALSE
);
$this->input->set_cookie($userName);

$password = array(
'name' => 'password',
'value' => YOURPASSWORD,
'expire' => '86500',
'prefix' => '',
'secure' => FALSE
);
$this->input->set_cookie($password);
}
redirect('dashboardv1');
}
}

Get the cookie you can use below code

<?php echo get_cookie('email'); ?>
<?php echo get_cookie('password'); ?>


Related Topics



Leave a reply



Submit