Application: How to Create Login and Registration Application with SQLite Database in Android Studio with Source Code

login and registration form in android studio

login and registration form in android studio

About How to Create Login and Registration Application with SQLite Database in Android Studio

In this tutorial, we will try on How to Create a Login and Registration Application with SQLite Database in Android Studio. This simple application can be used in any system that needed login verification. Android is a mobile operating system developed by Google. It is used in several gadgets like smartphones, tablets, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provides an adaptive framework that allows the developer to create an app more simply. So let’s now do the coding…

Getting Started:

First, you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

Next is the creation of activity_dashboard.xml after successfully logging in with the user.

Then after that create a new empty activity called Login. It will also created a new java file called Register. Locate the new layout file called activity_register.xml and write these code inside the file.

Android Manifest File

READ ALSO: E-Commerce App in Android Studio

The Android Manifest file provides essential information about your app to the Android system in which the system must require before running the code. It describes the overall information about the application. It contains some libraries that needed to access the several methods within the app.

Creating The Database

This code contain the script for creating a database and a database connection. To create this first create a new java file called DatabaseHelper, open the newly created file then extend the class by adding SQLiteOpenHelper. After that write these block of codes inside the DatabaseHelper class.

package com.example.shashank.login; /** * Created by Shashank on 14-Feb-18. */ import android.content.Context; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase; public class SQLiteHelper extends SQLiteOpenHelper < static String DATABASE_NAME="UserDataBase"; public static final String TABLE_NAME="UserTable"; public static final String Table_Column_ID="id"; public static final String Table_Column_1_Name="name"; public static final String Table_Column_2_Email="email"; public static final String Table_Column_3_Password="password"; public SQLiteHelper(Context context) < super(context, DATABASE_NAME, null, 1); >@Override public void onCreate(SQLiteDatabase database) < String CREATE_TABLE="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("+Table_Column_ID+" INTEGER PRIMARY KEY, "+Table_Column_1_Name+" VARCHAR, "+Table_Column_2_Email+" VARCHAR, "+Table_Column_3_Password+" VARCHAR)"; database.execSQL(CREATE_TABLE); >@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) < db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); onCreate(db); >>

Creating the Registration

This code contains the register function of the application. This will insert the data to the database by calling the DatabaseHelper when the button is clicked. To do that just write these block of codes inside the MainActivity class.

package com.example.shashank.login; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity < Button LogInButton, RegisterButton ; EditText Email, Password ; String EmailHolder, PasswordHolder; Boolean EditTextEmptyHolder; SQLiteDatabase sqLiteDatabaseObj; SQLiteHelper sqLiteHelper; Cursor cursor; String TempPassword = "NOT_FOUND" ; public static final String UserEmail = ""; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LogInButton = (Button)findViewById(R.id.buttonLogin); RegisterButton = (Button)findViewById(R.id.buttonRegister); Email = (EditText)findViewById(R.id.editEmail); Password = (EditText)findViewById(R.id.editPassword); sqLiteHelper = new SQLiteHelper(this); //Adding click listener to log in button. LogInButton.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < // Calling EditText is empty or no method. CheckEditTextStatus(); // Calling login method. LoginFunction(); >>); // Adding click listener to register button. RegisterButton.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < // Opening new user registration activity using intent on button click. Intent intent = new Intent(MainActivity.this, RegisterActivity.class); startActivity(intent); >>); > // Login function starts from here. public void LoginFunction()< if(EditTextEmptyHolder) < // Opening SQLite database write permission. sqLiteDatabaseObj = sqLiteHelper.getWritableDatabase(); // Adding search email query to cursor. cursor = sqLiteDatabaseObj.query(SQLiteHelper.TABLE_NAME, null, " " + SQLiteHelper.Table_Column_2_Email + "=?", new String[], null, null, null); while (cursor.moveToNext()) < if (cursor.isFirst()) < cursor.moveToFirst(); // Storing Password associated with entered email. TempPassword = cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table_Column_3_Password)); // Closing cursor. cursor.close(); >> // Calling method to check final result .. CheckFinalResult(); > else < //If any of login EditText empty then this block will be executed. Toast.makeText(MainActivity.this,"Please Enter UserName or Password.",Toast.LENGTH_LONG).show(); >> // Checking EditText is empty or not. public void CheckEditTextStatus() < // Getting value from All EditText and storing into String Variables. EmailHolder = Email.getText().toString(); PasswordHolder = Password.getText().toString(); // Checking EditText is empty or no using TextUtils. if( TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder))< EditTextEmptyHolder = false ; >else < EditTextEmptyHolder = true ; >> // Checking entered password from SQLite database email associated password. public void CheckFinalResult() < if(TempPassword.equalsIgnoreCase(PasswordHolder)) < Toast.makeText(MainActivity.this,"Login Successful",Toast.LENGTH_LONG).show(); // Going to Dashboard activity after login success message. Intent intent = new Intent(MainActivity.this, DashboardActivity.class); // Sending Email to Dashboard Activity using intent. intent.putExtra(UserEmail, EmailHolder); startActivity(intent); >else < Toast.makeText(MainActivity.this,"UserName or Password is Wrong, Please Try Again.",Toast.LENGTH_LONG).show(); >TempPassword = "NOT_FOUND" ; > >

Creating the Registration and Login

READ ALSO: Android Status App With Reward Point and Admin Panel (Lucky Wheel, WA Status Saver, Video, GIF, Quotes & Image)

This code contains the login function for the application. This code will read the entry field data then check the data if it exist in the DatabaseHelper class. To do that simply write these block of codes inside the RegisterActivity class.

package com.example.shashank.login; /** * Created by Shashank on 14-Feb-18. */ import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class RegisterActivity extends AppCompatActivity < EditText Email, Password, Name ; Button Register; String NameHolder, EmailHolder, PasswordHolder; Boolean EditTextEmptyHolder; SQLiteDatabase sqLiteDatabaseObj; String SQLiteDataBaseQueryHolder ; SQLiteHelper sqLiteHelper; Cursor cursor; String F_Result = "Not_Found"; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); Register = (Button)findViewById(R.id.buttonRegister); Email = (EditText)findViewById(R.id.editEmail); Password = (EditText)findViewById(R.id.editPassword); Name = (EditText)findViewById(R.id.editName); sqLiteHelper = new SQLiteHelper(this); // Adding click listener to register button. Register.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < // Creating SQLite database if dose n't exists SQLiteDataBaseBuild(); // Creating SQLite table if dose n't exists. SQLiteTableBuild(); // Checking EditText is empty or Not. CheckEditTextStatus(); // Method to check Email is already exists or not. CheckingEmailAlreadyExistsOrNot(); // Empty EditText After done inserting process. EmptyEditTextAfterDataInsert(); >>); > // SQLite database build method. public void SQLiteDataBaseBuild() < sqLiteDatabaseObj = openOrCreateDatabase(SQLiteHelper.DATABASE_NAME, Context.MODE_PRIVATE, null); >// SQLite table build method. public void SQLiteTableBuild() < sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS " + SQLiteHelper.TABLE_NAME + "(" + SQLiteHelper.Table_Column_ID + " PRIMARY KEY AUTOINCREMENT NOT NULL, " + SQLiteHelper.Table_Column_1_Name + " VARCHAR, " + SQLiteHelper.Table_Column_2_Email + " VARCHAR, " + SQLiteHelper.Table_Column_3_Password + " VARCHAR);"); >// Insert data into SQLite database method. public void InsertDataIntoSQLiteDatabase() < // If editText is not empty then this block will executed. if(EditTextEmptyHolder == true) < // SQLite query to insert data into table. SQLiteDataBaseQueryHolder = "INSERT INTO "+SQLiteHelper.TABLE_NAME+" (name,email,password) VALUES('"+NameHolder+"', '"+EmailHolder+"', '"+PasswordHolder+"');"; // Executing query. sqLiteDatabaseObj.execSQL(SQLiteDataBaseQueryHolder); // Closing SQLite database object. sqLiteDatabaseObj.close(); // Printing toast message after done inserting. Toast.makeText(RegisterActivity.this,"User Registered Successfully", Toast.LENGTH_LONG).show(); >// This block will execute if any of the registration EditText is empty. else < // Printing toast message if any of EditText is empty. Toast.makeText(RegisterActivity.this,"Please Fill All The Required Fields.", Toast.LENGTH_LONG).show(); >> // Empty edittext after done inserting process method. public void EmptyEditTextAfterDataInsert() < Name.getText().clear(); Email.getText().clear(); Password.getText().clear(); >// Method to check EditText is empty or Not. public void CheckEditTextStatus() < // Getting value from All EditText and storing into String Variables. NameHolder = Name.getText().toString() ; EmailHolder = Email.getText().toString(); PasswordHolder = Password.getText().toString(); if(TextUtils.isEmpty(NameHolder) || TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder))< EditTextEmptyHolder = false ; >else < EditTextEmptyHolder = true ; >> // Checking Email is already exists or not. public void CheckingEmailAlreadyExistsOrNot()< // Opening SQLite database write permission. sqLiteDatabaseObj = sqLiteHelper.getWritableDatabase(); // Adding search email query to cursor. cursor = sqLiteDatabaseObj.query(SQLiteHelper.TABLE_NAME, null, " " + SQLiteHelper.Table_Column_2_Email + "=?", new String[], null, null, null); while (cursor.moveToNext()) < if (cursor.isFirst()) < cursor.moveToFirst(); // If Email is already exists then Result variable value set as Email Found. F_Result = "Email Found"; // Closing cursor. cursor.close(); >> // Calling method to check final result and insert data into SQLite database. CheckFinalResult(); > // Checking result public void CheckFinalResult() < // Checking whether email is already exists or not. if(F_Result.equalsIgnoreCase("Email Found")) < // If email is exists then toast msg will display. Toast.makeText(RegisterActivity.this,"Email Already Exists",Toast.LENGTH_LONG).show(); >else < // If email already dose n't exists then user registration details will entered to SQLite database. InsertDataIntoSQLiteDatabase(); >F_Result = "Not_Found" ; > >

And lastly, put this code after successfully loging in, in the DashboardActivity class.

package com.example.shashank.login; /** * Created by Shashank on 14-Feb-18. */ import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class DashboardActivity extends AppCompatActivity < String EmailHolder; TextView Email; Button LogOUT ; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_dashboard); Email = (TextView)findViewById(R.id.textView1); LogOUT = (Button)findViewById(R.id.button1); Intent intent = getIntent(); // Receiving User Email Send By MainActivity. EmailHolder = intent.getStringExtra(MainActivity.UserEmail); // Setting up received email to TextView. Email.setText(Email.getText().toString()+ EmailHolder); // Adding click listener to Log Out button. LogOUT.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View view) < //Finishing current DashBoard activity on button click. finish(); Toast.makeText(DashboardActivity.this,"Log Out Successful", Toast.LENGTH_LONG).show(); >>); > >

Try to run the app and see if it worked.

READ ALSO: Two Way Text Chat via Bluetooth in Android Studio

Screenshot

login and registration application with sqlite database in android studio

login and registration application with sqlite database in android studio

DEMO

There you have it we have created a Login and Registration Application with SQLite Database in Android Studio . I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!