Form validation on Button click in android -
i'm developing android app in login activity has edittext,radiobutton,spinner , button.so when button pressed i've validate form checking whether fields filled else alert message given user.can please me java code. in advance.
public void onclicklistener(view v) { name=(edittext)findviewbyid(r.id.edittext1); years1=(radiobutton)findviewbyid(r.id.radiobutton3); years2=(radiobutton)findviewbyid(r.id.radiobutton4); years3=(radiobutton)findviewbyid(r.id.radiobutton5); manager=(radiobutton)findviewbyid(r.id.radiobutton1); teamleader=(radiobutton)findviewbyid(r.id.radiobutton2); rg1=(radiogroup)findviewbyid(r.id.designation); rg2=(radiogroup)findviewbyid(r.id.years); button proceed = (button) findviewbyid(r.id.button1); proceed.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (validationsuccess()) { } else if(manager.ischecked()) { intent managerintent = new intent(getapplicationcontext(), managerquestionnaire1.class); // <----- start "search" activity startactivityforresult(managerintent, 0); } else if(teamleader.ischecked()) { intent teamleaderintent = new intent(getapplicationcontext(), teamleaderquestionnaire1.class); // <----- start "type entries out" activity startactivityforresult(teamleaderintent, 0); } else { alertdialog(); } } }); } private boolean validationsuccess() { if(name.gettext().tostring().equalsignorecase("")) { toast.maketext(getapplicationcontext(),"please enter name",0).show(); return false; } if(rg1.getcheckedradiobuttonid()<=0) { toast.maketext(getapplicationcontext(),"please select designation",0).show(); return false; } if(rg2.getcheckedradiobuttonid()<=0) { toast.maketext(getapplicationcontext(),"please select experience",0).show(); return false; } if(dept.getselecteditemposition()==0) { toast.maketext(getapplicationcontext(),"please select depatrment",0).show(); return false; } return true; } private void alertdialog() { alertdialog.builder alertdialogbuilder = new alertdialog.builder(login.this); alertdialogbuilder.setmessage("please ensure questions answered").setcancelable(false).setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { dialog.cancel(); } }); alertdialog alert = alertdialogbuilder.create(); alert.show(); }
use below code:
mbtnnext.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (validationsuccess()) { intent mintent = new intent( youractivity.this, newactivity.class); } else { alertdialog(); } }); private boolean validationsuccess(){ if(editusername.gettext().tostring().equalsignorecase("")){ toast.maketext(getapplicationcontext();"please enter name",0).show(); return false; } if(radiogroupdesignation.getcheckedradiobuttonid()<=0){ toast.maketext(getapplicationcontext();"please select designation",0).show(); return false; } if(radiogroupexp.getcheckedradiobuttonid()<=0){ toast.maketext(getapplicationcontext();"please select experience",0).show(); return false; } if(spinner.getselecteditemposition()==0){ toast.maketext(getapplicationcontext();"please select depatrment",0).show(); return false; } return true; }
Comments
Post a Comment