Android Login and Register with SQLite Database Tutorial [With Source Code]
To create login and Registraion form in android, we need a class which manage our SQLite Database and table.
For this create new project and then add one class by right click the mainactivity>new>class. Then give the name of the class as DatabaseHelper. Now extends this class with SQLiteOpenHelper. Then implement the methods and constructor of SQLiteOpenHelper. For this, focus the cursur in the class and press alt+enter.
Now write the following code to design schema.
[sociallocker]
Schema define how database is organized. it is just like a SQL statements we use to create database and table.
public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME="register.db"; public static final String TABLE_NAME="registeration"; public static final String COL_1="ID"; public static final String COL_2="FirstName"; public static final String COL_3="LastName"; public static final String COL_4="Password"; public static final String COL_5="Email"; public static final String COL_6="Phone"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,FirstName TEXT,LastName TEXT,Password TEXT,Email TEXT,Phone TEXT)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS" +TABLE_NAME); //Drop older table if exists onCreate(db); } }
Now design the Registraion form in activity_main.xml file as show in video. watch the video for this purpose. After completing the design part go back to MainActivity.java and write the following code.
1. Declare the button and textfieldas and sqlitedatabase and sqliteopenhelper inside the class as:
public class MainActivity extends AppCompatActivity { SQLiteOpenHelper openHelper; SQLiteDatabase db; Button _btnreg, _btnlogin; EditText _txtfanme, _txtlname, _txtpass, _txtemail, _txtphone;
2. Inside the onCreate method initialize all the button, textfield and call the databasehelper class as:
openHelper = new DataBaseHelper(this); _txtfname = (EditText)findViewById(R.id.txtfname); _txtlname = (EditText)findViewById(R.id.txtlname); _txtpass = (EditText)findViewById(R.id.txtpass); _txtemail = (EditText)findViewById(R.id.txtemail); _txtphone = (EditText)findViewById(R.id.txtphone); _btnlogin=(Button)findViewById(R.id.btnlogin); _btnreg=(Button)findViewById(R.id.btnreg);
3. Create the listening event of _btnreg as follows and write the code inside that listening event of the button.
_btnreg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { db=openHelper.getWritableDatabase(); String fname=_txtfanme.getText().toString(); String lname=_txtlname.getText().toString(); String pass=_txtpass.getText().toString(); String email=_txtemail.getText().toString(); String phone=_txtphone.getText().toString(); insertdata(fname, lname,pass,email,phone); Toast.makeText(getApplicationContext(), "register successfully",Toast.LENGTH_LONG).show(); } });
4. Now create one method having the name insertdata. as follows and write the following code inside that method.
public void insertdata(String fname, String lname, String pass, String email, String phone){ ContentValues contentValues = new ContentValues(); contentValues.put(DatabaseHelper.COL_2, fname); contentValues.put(DatabaseHelper.COL_3, lname); contentValues.put(DatabaseHelper.COL_4, pass); contentValues.put(DatabaseHelper.COL_5, email); contentValues.put(DatabaseHelper.COL_6, phone); long id = db.insert(DatabaseHelper.TABLE_NAME, null, contentValues); } });
This parts complete the registration part now we need to move on towards the login part.
1. Create the listening event of the button _btnlogin as follow and write the code as:
_btnlogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, login.class); startActivity(intent); } }); } });
2.Add one empty activity by right clicking the MainActivity.java file as MainActivity>new>Activity>EmptyActivity and give the name of this activity whatever you want but i am giving the name login.java.
3. Go to xml file of this recently added(login.java) activity xml file and desing the login form see the video for this.
4. Go back to the login.java and write the following code.
a. Declare the textfield, button, sqlitedatabase and sqliteopenhelper class inside the login class as:
SQLiteDatabase db; SQLiteOpenHelper openHelper; Cursor cursor; Button _btnLogin; EditText _txtEmail, _txtPass;
b. Initialize the button and textfield and call the databasehelper class inside the onCreate methods as:
_txtEmail=(EditText)findViewById(R.id.txtEmail); _txtPass=(EditText)findViewById(R.id.txtPass); _btnLogin=(Button)findViewById(R.id.btnLogin); openHelper=new DataBaseHelper(this); db = openHelper.getReadableDatabase();
c. Create the listening event of the button and write the following code inside the listening event of the button.
_btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String email = _txtEmail.getText().toString(); String pass = _txtPass.getText().toString(); cursor = db.rawQuery("SELECT *FROM " + DataBaseHelper.TABLE_NAME + " WHERE " + DataBaseHelper.COL_5 + "=? AND " + DataBaseHelper.COL_4 + "=?", new String[]{email, pass}); if (cursor != null) { if (cursor.getCount() > 0) { Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "Login error", Toast.LENGTH_SHORT).show(); } } } });
That’s it guyz.
[/sociallocker]
if this article help you stay connect with us and don’t forget to watch the following tutorial video because you will not understand this whole tutorial without watching this video.