SIgn IN Sign Up with Sqlite in android .Unable to solve -
i creating sign-in sign sqlite unable sign in. while sign working. please me problem? signup working fine unable click sign in.
mainactivity
import android.app.activity; import android.app.dialog; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class mainactivity extends activity { edittext edittextusername, edittextpassword; button btnsignin, btnsignup; // string username,password; logindatabaseadapter logindatabaseadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // create instance of sqlite database logindatabaseadapter = new logindatabaseadapter(this); logindatabaseadapter = logindatabaseadapter.open(); btnsignin = (button) findviewbyid(r.id.buttonsignin); btnsignup = (button) findviewbyid(r.id.buttonsignup); // refference of buttons // set onclick listener on signup button btnsignup.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub /// create intent signupactivity abd start activity intent intentsignup = new intent(getapplicationcontext(), signupactivity.class); startactivity(intentsignup); } }); } // methos handleclick event of sign in button public void signin(view v) { final dialog dialog = new dialog(mainactivity.this); dialog.setcontentview(r.layout.signin); dialog.settitle("login"); // refferences of views final edittext edittextusername=(edittext)dialog.findviewbyid(r.id.edittextusernametologin); final edittext edittextpassword=(edittext)dialog.findviewbyid(r.id.edittextpasswordtologin); button btnsignin=(button)dialog.findviewbyid(r.id.buttonsignin); // set on clicklistener btnsignin.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // user name , password string username=edittextusername.gettext().tostring(); string password=edittextpassword.gettext().tostring(); // fetch password form database respective user name string storedpassword=logindatabaseadapter.getsinlgeentry(username); // check if stored password matches password entered user if(password.equals(storedpassword)) { toast.maketext(mainactivity.this, "congrats: login successfull", toast.length_long).show(); dialog.dismiss(); } else { toast.maketext(mainactivity.this, "user name or password not match", toast.length_long).show(); } } }); dialog.show(); } @override protected void ondestroy() { super.ondestroy(); // close database logindatabaseadapter.close(); } }
activity_main
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <textview android:id="@+id/mobileno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="60dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/registeresd_mobile_number"/> <edittext android:id="@+id/edittextusername" android:hint="user name" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="15dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> </edittext> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_margintop="20dp" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <textview android:id="@+id/edittextpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/password"/> <edittext android:id="@+id/edittextpasswordtologin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_gravity="center" android:maxlength="20" android:inputtype="textpassword" android:hint="password" /> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_margintop="20dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <button android:id="@+id/buttonsignin" android:layout_margintop="20dp" android:color="#7ec0ee" android:layout_width="fill_parent" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_height="wrap_content" android:background="@drawable/rounded_edittext" android:clickable="true" android:text="sign in" /> <textview android:id="@+id/signup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="15dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:clickable="true" android:text="@string/signup"/> <button android:id="@+id/buttonsignup" android:layout_margintop="20dp" android:color="#7ec0ee" android:layout_width="fill_parent" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_height="wrap_content" android:background="@drawable/rounded_edittext" android:text="sign up" /> </linearlayout>
signup
<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:fillviewport="false" > <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical" > <textview android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/username"/> <edittext android:id="@+id/edittextusername" android:hint="user name" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="15dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> </edittext> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_margintop="20dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <textview android:id="@+id/email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/email"/> <edittext android:id="@+id/emailid" android:hint="emailid" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="15dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> </edittext> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_margintop="20dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <textview android:id="@+id/contact" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/contact"/> <edittext android:id="@+id/contactnumber" android:hint="contact number" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="15dp" android:maxlength="10" android:inputtype="number" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content"> </edittext> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_margintop="20dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <textview android:id="@+id/password1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/password1"/> <edittext android:id="@+id/edittextpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="15dp" android:maxlength="20" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:hint="password" android:inputtype="textpassword" /> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_margintop="20dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <textview android:id="@+id/confirmpassword1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/confirmpassword1"/> <edittext android:id="@+id/edittextconfirmpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_marginright="20dp" android:hint="confirm password" android:maxlength="20" android:inputtype="textpassword" /> <button android:id="@+id/buttoncreateaccount" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="create account" android:layout_marginbottom="60dp" android:layout_marginleft="20dp" android:layout_margintop="20dp" android:layout_marginright="20dp" android:background="@drawable/rounded_edittext"/> </linearlayout> </scrollview>
signup
import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class signupactivity extends activity { edittext edittextusername,edittextpassword,edittextconfirmpassword,emailid,contactnumber; button btncreateaccount; logindatabaseadapter logindatabaseadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.signup); // instance of database adapter logindatabaseadapter=new logindatabaseadapter(this); logindatabaseadapter=logindatabaseadapter.open(); // refferences of views edittextusername=(edittext)findviewbyid(r.id.edittextusername); edittextpassword=(edittext)findviewbyid(r.id.edittextpassword); edittextconfirmpassword=(edittext)findviewbyid(r.id.edittextconfirmpassword); emailid=(edittext)findviewbyid(r.id.emailid); contactnumber=(edittext)findviewbyid(r.id.contactnumber); btncreateaccount=(button)findviewbyid(r.id.buttoncreateaccount); btncreateaccount.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub string username=edittextusername.gettext().tostring(); string password=edittextpassword.gettext().tostring(); string confirmpassword=edittextconfirmpassword.gettext().tostring(); string id=emailid.gettext().tostring(); string number= contactnumber.gettext().tostring(); // check if of fields vaccant if(username.equals("")||password.equals("")||confirmpassword.equals("")) { toast.maketext(getapplicationcontext(), "field vaccant", toast.length_long).show(); return; } // check if both password matches if(!password.equals(confirmpassword)) { toast.maketext(getapplicationcontext(), "password not match", toast.length_long).show(); return; } /* if(!emailid(isvalidemail(charsequence target))) { }*/ else { // save data in database logindatabaseadapter.insertentry(number, password); toast.maketext(getapplicationcontext(), "account created ", toast.length_long).show(); } } }); } public final static boolean isvalidemail(charsequence target) { if (target == null) { return false; } else { return android.util.patterns.email_address.matcher(target).matches(); } } @override protected void ondestroy() { // todo auto-generated method stub super.ondestroy(); logindatabaseadapter.close(); } }
logindatabaseadapter
import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; public class logindatabaseadapter { static final string database_name = "login.db"; static final int database_version = 1; public static final int name_column = 1; // todo: create public field each column in table. // sql statement create new database. static final string database_create = "create table "+"login"+ "( " +"id"+" integer primary key autoincrement,"+ "username text,password text); "; // variable hold database instance public sqlitedatabase db; // context of application using database. private final context context; // database open/upgrade helper private databasehelper dbhelper; public logindatabaseadapter(context _context) { context = _context; dbhelper = new databasehelper(context, database_name, null, database_version); } public logindatabaseadapter open() throws sqlexception { db = dbhelper.getwritabledatabase(); return this; } public void close() { db.close(); } public sqlitedatabase getdatabaseinstance() { return db; } public void insertentry(string username,string password) { contentvalues newvalues = new contentvalues(); // assign values each row. newvalues.put("username", username); newvalues.put("password",password); // insert row table db.insert("login", null, newvalues); ///toast.maketext(context, "reminder saved", toast.length_long).show(); } public int deleteentry(string username) { //string id=string.valueof(id); string where="username=?"; int numberofentriesdeleted= db.delete("login", where, new string[]{username}) ; // toast.maketext(context, "number fo entry deleted : "+numberofentriesdeleted, toast.length_long).show(); return numberofentriesdeleted; } public string getsinlgeentry(string username) { cursor cursor=db.query("login", null, " username=?", new string[]{username}, null, null, null); if(cursor.getcount()<1) // username not exist { cursor.close(); return "not exist"; } cursor.movetofirst(); string password= cursor.getstring(cursor.getcolumnindex("password")); cursor.close(); return password; } public void updateentry(string username,string password) { // define updated row content. contentvalues updatedvalues = new contentvalues(); // assign values each row. updatedvalues.put("username", username); updatedvalues.put("password",password); string where="username = ?"; db.update("login",updatedvalues, where, new string[]{username}); } }
databasehelper
import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; /** * created neha thakur on 5/23/2016. */ public class databasehelper extends sqliteopenhelper { public databasehelper(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, name, factory, version); } // called when no database exists in disk , helper class needs // create new one. @override public void oncreate(sqlitedatabase _db) { _db.execsql(logindatabaseadapter.database_create); } // called when there database version mismatch meaning version // of database on disk needs upgraded current version. @override public void onupgrade(sqlitedatabase _db, int _oldversion, int _newversion) { // log version upgrade. log.w("taskdbadapter", "upgrading version " +_oldversion + " " +_newversion + ", destroy old data"); // upgrade existing database conform new version. multiple // previous versions can handled comparing _oldversion , _newversion // values. // simplest case drop old table , create new one. _db.execsql("drop table if exists " + "template"); // create new one. oncreate(_db); } }
signin
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <textview android:id="@+id/mobileno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="60dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/registeresd_mobile_number"/> <edittext android:id="@+id/edittextusernametologin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_gravity="center" android:inputtype="number" android:maxlength="10" android:hint="mobile number"> </edittext> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_margintop="20dp" android:layout_marginleft="15dp" android:layout_marginright="15dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <textview android:id="@+id/edittextpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_gravity="center" android:textsize="20dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:text="@string/password"/> <edittext android:id="@+id/edittextpasswordtologin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_gravity="center" android:maxlength="20" android:inputtype="textpassword" android:hint="password" /> <view android:layout_width="300dp" android:layout_height="2dp" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_margintop="20dp" android:layout_gravity="center" android:background="#c0c0c0"></view> <button android:id="@+id/buttonsignin" android:layout_margintop="20dp" android:color="#7ec0ee" android:layout_width="fill_parent" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_height="wrap_content" android:background="@drawable/rounded_edittext" android:text="sign in" /> </linearlayout>
add onclick method signin button.
<button android:id="@+id/buttonsignin" android:layout_margintop="20dp" android:color="#7ec0ee" android:layout_width="fill_parent" android:layout_marginleft="20dp" android:layout_marginright="20dp" android:layout_height="wrap_content" android:background="@drawable/rounded_edittext" <!-- here --> android:onclick="signin" android:clickable="true" android:text="sign in" />
Comments
Post a Comment