Android "wrong password or username " message box display error -


i working on activity has username , password. used alertdialog.builder in else part show message of "wrong password or username". not connecting database test purpose using strings password , username compare values of edittext fields with, in if condition , if match takes user new screen name "newmenu". problem when login activity starts (the app runs), shows first of message, else part, want show message after submit button has been clicked , the password or username wrong. here code

public class login extends activity{       private string pass=new string();     private string nam=new string();     private button log;     private view textreg;     private edittext text1;     private edittext text2;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.login);         textreg=(textview) findviewbyid(r.id.textregister);         log=(button) findviewbyid(r.id.btnsubmit);         text1=(edittext) findviewbyid(r.id.txtuname);         text2=(edittext) findviewbyid(r.id.txtpassword);                  //if not registered users// ////a textview when clicked takes user registeration page/////          textreg.setonclicklistener(new onclicklistener() {              @override                     public void onclick(view view) {                 startactivity(new intent("com.drms.register"));                     }                 });            //                                                        //         // if password , username correct, goto newmenu activity //        //                                                        //          pass="12345";         nam="12345";          if(text1.gettext().tostring().equals(nam) && text2.gettext().tostring().equals(pass) ){               log.setonclicklistener(new onclicklistener() {                  @override                          public void onclick(view view) {                     startactivity(new intent("com.drms.newmenu"));                         }                     });          }              else{             alertdialog.builder dlgalert  = new alertdialog.builder(this);              dlgalert.setmessage("wrong password or username");             dlgalert.settitle("error message...");             dlgalert.setpositivebutton("ok", null);             dlgalert.setcancelable(true);             dlgalert.create().show();              dlgalert.setpositivebutton("ok",                     new dialoginterface.onclicklistener() {                         public void onclick(dialoginterface dialog, int which) {                          }                     });             }     } } 

your algorithm not ok. oncreate() called when activity created, check username , password @ each creation. never set text fields, values never ok @ first launch, show error dialog @ first launch. should make :

protected void oncreate(bundle savedinstancestate) {      // 1. call super() , init fields      string pass="12345";     string nam="12345";      // 2. set onclicklistener     textreg.setonclicklistener(new onclicklistener() {          @override         public void onclick(view view) {             startactivity(new intent("com.drms.register"));         }     });      // 3. set onclicklistener login button     log.setonclicklistener(new onclicklistener() {          @override         public void onclick(view view) {             // 4. check if fields null             if(text1.gettext() != null && text2.gettext() != null) {                  // 4.a check if username , password ok                 if(text1.gettext().tostring().equals(nam) &&                      text2.gettext().tostring().equals(pass) )                 {                         // not null , ok, launch activity                                 startactivity(new intent("com.drms.newmenu"));                 } else {                     // username or password false, display , error                     alertdialog.builder dlgalert  = new alertdialog.builder(this);                      dlgalert.setmessage("wrong password or username");                     dlgalert.settitle("error message...");                     dlgalert.setpositivebutton("ok", null);                     dlgalert.setcancelable(true);                     dlgalert.create().show();                      dlgalert.setpositivebutton("ok",                         new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int which) {                          }                     });                 }             } else {                 // fields not filled.                 // display error message "please enter username/password             }         }     }); } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -