android - Add a "Remember me" checkbox -


i have checkbox button remember user id , password. can please guide me in right direction how started?

i built app, here's basic code , explanation:

basically key here sharedpreferences. you'll setup sharedpreferences object , store username , password after user has entered them. then, when run application again, preferences have data stored , repopulate 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 @ end, dosomethingelse() placeholder go next step application. dosomethingelse() method loads activity.

here's basic xml file 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 want encrypt password before storing in sharedpreferences. details beyond scope of question, here code used that: http://www.androidsnippets.com/encryptdecrypt-strings. you'll have come kind of key schema too.

this code has been tested on android 2.1, sdk 7. let me know how works you.

david


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -