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. 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);

public void onCreate(SQLiteDatabase db) {
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" +TABLE_NAME); //Drop older table if exists

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 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(;
_txtlname = (EditText)findViewById(;
_txtpass = (EditText)findViewById(;
_txtemail = (EditText)findViewById(;
_txtphone = (EditText)findViewById(;

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() {
public void onClick(View v) {
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() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, login.class);

2.Add one empty activity by right clicking the file as MainActivity>new>Activity>EmptyActivity and give the name of this activity whatever you want but i am giving the name

3. Go to xml file of this recently added( activity xml file and desing the login form see the video for this.

4. Go back to the 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:

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() {
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. 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.