Login and Registration in Android and SQLite. In this article, we will look at an example to perform Login and Registration in Android using SQLite.
The Android SDK that we are going to use here is the set of development tools to develop applications for the android platform. The database we are using is SQLite that comes with android. So, let us start with the development.
Following will be the project structure:
Step1: Getting Started With Android Studio
- Create a project with Empty activity.
- In this activity, we will Create the Main Page of our Application
- This page consists of two-button and Each Button linked to other Activity
- Those Two other Activities are SignUp and Login Activity
- findViewById is used to assign View to Object
- On Click, Listener is set to login button when we click on login button following command executed
- We create new intent with the context of this and login class to switch to Login class when startActivity(intent) is executed.
MainActivity.class
package com.example.login_reg; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.example.login_reg.Sql.DBHelper; public class MainActivity extends AppCompatActivity { Button login,Reg; Toolbar toolbar; DBHelper dbHelper; @Override public void onBackPressed() { MainActivity.this.finish(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new DBHelper(this); login =(Button) findViewById(R.id.btnLogin); toolbar = (Toolbar) findViewById(R.id.tool_main); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,Login.class); startActivity(intent); } }); Reg = findViewById(R.id.btnSignUp); Reg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this,SignUp.class); startActivity(intent); } }); }
activity_main.xml
- XML consist of Relative layout
- include statement is used to include toolbar to the main activity
- android: id is used to create a unique id for Views and Layout
- Id is used to connect View to variables
- android: background is assigned a theme to Views.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@drawable/background_main" app:layout_goneMarginBottom="10dp"> <include layout="@layout/toolbar" android:id="@+id/tool_main"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:ignore="MissingConstraints" > <ImageView android:id="@+id/imageView" android:layout_width="150dp" android:layout_height="150dp" android:layout_marginBottom="10dp" app:tint="#000000" tools:srcCompat="@drawable/logo" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginBottom="10dp" android:fontFamily="cursive" android:gravity="center" android:padding="10dp" android:text="Code Bun" android:textSize="60sp" /> <Button android:id="@+id/btnLogin" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@drawable/btn_view" android:fontFamily="sans-serif-black" android:padding="10dp" android:text="Login" android:textColor="#1B1515" android:textSize="18sp" /> <Button android:id="@+id/btnSignUp" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@drawable/btn_view" android:fontFamily="sans-serif-black" android:padding="10dp" android:text="Sign Up" android:textColor="#1B1515" android:textSize="18sp" /> </LinearLayout> </RelativeLayout>
Step 2: Create a New Activity for Login and SignUp page
Right-click on the com.example.<app-name> and create three new empty activity with name Login,SignUp and FinalPage.
- Each of these activities is for a different Task
- Login.class is for login page it consists of the Image view, two edit text, button and a text view that is linked to SingUp activity
- The cursor is used to get Data from Database Row by Row
- Toast is a popup message
- LoginCheck is a boolean method that will return true if Credentials Match and return false if it doesn’t
- The intent is used to jump to other activities with the context of this class to Reference Class
- StartActivity is used to jump to another activity
- AlertDialog is a Dialog Box that will display the message.
Login.class
package com.example.login_reg; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.login_reg.Sql.DBHelper; public class Login extends AppCompatActivity { EditText email , password; Button btnSubmit; TextView createAcc; DBHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Boolean e=false,p=false; setContentView(R.layout.activity_login); email=findViewById(R.id.text_email); password=findViewById(R.id.text_pass); btnSubmit = findViewById(R.id.btnSubmit_login); dbHelper = new DBHelper(this); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String emailCheck = email.getText().toString(); String passCheck = password.getText().toString(); Cursor cursor = dbHelper.getData(); if(cursor.getCount() == 0){ Toast.makeText(Login.this,"No entries Exists",Toast.LENGTH_LONG).show(); } if (loginCheck(cursor,emailCheck,passCheck)) { Intent intent = new Intent(Login.this,FinalPage.class); intent.putExtra("email",emailCheck); email.setText(""); password.setText(""); startActivity(intent); }else { AlertDialog.Builder builder = new AlertDialog.Builder(Login.this); builder.setCancelable(true); builder.setTitle("Wrong Credential"); builder.setMessage("Wrong Credential"); builder.show(); } dbHelper.close(); } }); createAcc=findViewById(R.id.createAcc); createAcc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Login.this,SignUp.class); startActivity(intent); } }); } public static boolean loginCheck(Cursor cursor,String emailCheck,String passCheck) { while (cursor.moveToNext()){ if (cursor.getString(0).equals(emailCheck)) { if (cursor.getString(2).equals(passCheck)) { return true; } return false; } } return false; } }
activity_login.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Login" android:background="@drawable/background_main"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="170dp" tools:layout_editor_absoluteY="74dp" android:gravity="center"> <ImageView android:id="@+id/imageView" android:layout_width="150dp" android:layout_height="150dp" app:tint="#000000" tools:srcCompat="@drawable/logo" android:layout_marginBottom="10dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="60sp" android:text="Code Bun" android:fontFamily="cursive" android:gravity="center" android:padding="10dp" android:layout_marginBottom="10dp" /> <EditText android:id="@+id/text_email" android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/edit_text_box" android:layout_marginBottom="10dp" android:inputType="textEmailAddress" android:hint="Enter Email" android:gravity="center" android:padding="20dp" android:drawableStart="@drawable/ic_mail" /> <EditText android:id="@+id/text_pass" android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/edit_text_box" android:layout_marginBottom="10dp" android:inputType="numberPassword" android:hint="Enter Password" android:gravity="center" android:padding="20dp" android:drawableStart="@drawable/ic_lock" /> <Button android:id="@+id/btnSubmit_login" android:layout_width="250dp" android:layout_height="60dp" android:layout_marginBottom="10dp" android:background="@drawable/btn_view" android:fontFamily="sans-serif-black" android:padding="10dp" android:text="Login" android:textColor="#1B1515" android:textSize="18sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <View android:layout_width="100dp" android:layout_height="2dp" android:background="@android:color/black"/> <TextView android:layout_width="25dp" android:layout_height="25dp" android:textSize="20dp" android:text="Or" android:gravity="center"/> <View android:layout_width="100dp" android:layout_height="2dp" android:background="@android:color/black"/> </LinearLayout> <TextView android:id="@+id/createAcc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Create an Account" android:clickable="true" android:textStyle="bold" android:textSize="20dp" /> </LinearLayout> </RelativeLayout>
SignUp.class
package com.example.login_reg; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.example.login_reg.Sql.DBHelper; public class SignUp extends AppCompatActivity { EditText name , number , email,pass; TextView login; DBHelper dbHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_up); name=findViewById(R.id.textName); number=findViewById(R.id.textNumber); email=findViewById(R.id.textEmail); pass=findViewById(R.id.textPass); Button signUpAcc = findViewById(R.id.btnSignUpAcc); dbHelper = new DBHelper(this); signUpAcc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String name1 = name.getText().toString(); String number1 = number.getText().toString(); String email1 = email.getText().toString(); String pass1 = pass.getText().toString(); boolean b =dbHelper.insetUserData(name1,number1,email1,pass1); if (b){ Toast.makeText(SignUp.this,"Data inserted",Toast.LENGTH_SHORT).show(); Intent i = new Intent(SignUp.this,Login.class); startActivity(i); }else { Toast.makeText(SignUp.this,"Failed To insert Data",Toast.LENGTH_SHORT).show(); } } }); login=findViewById(R.id.loginAcc); login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent i = new Intent(SignUp.this,Login.class); startActivity(i); } }); } }
activity_sign_up.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SignUp" android:background="@drawable/background_main" android:scrollbars="vertical"> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" tools:ignore="MissingConstraints" android:layout_gravity="center" android:gravity="center" android:scrollbars="vertical"> <ImageView android:id="@+id/imageView" android:layout_width="150dp" android:layout_height="150dp" app:tint="#000000" tools:srcCompat="@drawable/logo" android:layout_marginBottom="10dp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="60sp" android:text="Code Bun" android:fontFamily="cursive" android:gravity="center" android:padding="10dp" android:layout_marginBottom="10dp" /> <EditText android:id="@+id/textName" android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/edit_text_box" android:layout_marginBottom="10dp" android:inputType="textPersonName" android:hint="Name" android:gravity="center" android:padding="20dp" android:drawableStart="@drawable/ic_name" /> <EditText android:id="@+id/textNumber" android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/edit_text_box" android:layout_marginBottom="10dp" android:inputType="number" android:hint="Phone Number" android:gravity="center" android:padding="20dp" android:drawableStart="@drawable/ic_phone" /> <EditText android:id="@+id/textEmail" android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/edit_text_box" android:layout_marginBottom="10dp" android:inputType="textEmailAddress" android:hint="Email" android:gravity="center" android:padding="20dp" android:drawableStart="@drawable/ic_mail" /> <EditText android:id="@+id/textPass" android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/edit_text_box" android:layout_marginBottom="10dp" android:inputType="numberPassword" android:hint="Password" android:gravity="center" android:padding="20dp" android:drawableStart="@drawable/ic_lock" /> <Button android:id="@+id/btnSignUpAcc" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@drawable/btn_view" android:fontFamily="sans-serif-black" android:padding="10dp" android:text="Sign up" android:textColor="#1B1515" android:textSize="18sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <View android:layout_width="100dp" android:layout_height="2dp" android:background="@android:color/black"/> <TextView android:layout_width="25dp" android:layout_height="25dp" android:textSize="20dp" android:text="Or" android:gravity="center"/> <View android:layout_width="100dp" android:layout_height="2dp" android:background="@android:color/black"/> </LinearLayout> <TextView android:id="@+id/loginAcc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" android:clickable="true" android:textStyle="bold" android:textSize="20dp" /> </LinearLayout> </ScrollView> </RelativeLayout>
FinalPage.class
package com.example.login_reg; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class FinalPage extends AppCompatActivity { TextView text; @Override public void onBackPressed() { FinalPage.this.finish(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_final_page); text = findViewById(R.id.changeText); Intent intent = getIntent(); String s2 = intent.getStringExtra("email"); text.setText(s2); } }
activity_final_page.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FinalPage" android:background="@drawable/background_main"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:id="@+id/changeText" android:gravity="center" android:editable="false" android:background="@drawable/edit_text_box" android:padding="10dp"/> </LinearLayout> </RelativeLayout>
Step 3: Setting Up SQLite DataBase
Right-click on com. example and go to new and then package name it Sql then create a new java class with name DBHelper.
- First, we extend the class with SQLiteOpenHelper
- Here we create a Database with the help of SQLiteOpenHelper
- With the help of Constructor, we create a database with the name Userdata
- We have to implement two methods onCreate and onUpgrade these two are abstracts of class SQLiteOpenHelper
- onCreate method is used to create Table in Database we use object of SQLite class to access execSQL method
- In execSQL we pass Name of Table and Variable and their DataType
- insertUserData method is used to insert data into a data table
- DB.insert is used to insert data and return integer value -1 if the insertion failed.
DBHelper.class
package com.example.login_reg.Sql; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context ) { super(context,"UserData",null, 1); } @Override public void onCreate(SQLiteDatabase DB) { DB.execSQL("create Table UserDetails(userID TEXT primary key,name TEXT,password PASSWORD,number NUMBER)"); } @Override public void onUpgrade(SQLiteDatabase DB, int i, int i1) { DB.execSQL("drop Table if exists UserDetails"); } public Boolean insetUserData(String name,String number,String email,String password){ SQLiteDatabase DB = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("userID",email); contentValues.put("name",name); contentValues.put("password",password); contentValues.put("number",number); long result= DB.insert("UserDetails",null,contentValues); if (result == -1){ return false; }else { return true; } } public Cursor getData(){ SQLiteDatabase DB = this.getWritableDatabase(); Cursor cursor = DB.rawQuery("Select * from Userdetails ",null); return cursor; } }
Step 4: Creating Resource Files
Go to app -> res -> drawable and create these files.
Background_main.xml
- gradient is used to mix colors.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:angle="90" android:startColor="#A443E9" android:endColor="#38f9d7" android:type="linear"/> </shape> </item> </selector>
Btn_view.xml
- A shape tag is used to give shape to view.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true"> <shape android:shape="rectangle"> <corners android:radius="10dp"/> <gradient android:startColor="#A443E9" android:endColor="#38f9d7" android:type="linear"/> </shape> </item> </selector>
Edit_text_box.xml
- corners tag is used curve the view
- if it is a rectangle give it 10dp it will be more like ovel shape.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_focused="true"> <shape android:shape="rectangle"> <gradient android:startColor="#A443E9" android:endColor="#38f9d7" android:type="linear"/> <corners android:radius="10dp"/> </shape> </item> </selector>
ic_lock.xml
- Right-Click on Drawable select vector asset
- Search for Lock and select and rename it to ic_lock.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" android:tint="?attr/colorControlNormal"> <path android:fillColor="@android:color/white" android:pathData="M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0, -1.1 -0.9,-2 -2,-2zM12,17c-1.1,0 -2,-0.9 -2,-2s0.9,-2 2,-2 2,0.9 2,2 -0.9,2 -2,2zM15.1,8L8.9,8L8.9,6c0,-1.71 1.39,-3.1 3.1,-3.1 1.71,0 3.1,1.39 3.1,3.1v2z"/> </vector>
Ic_mail.xml
- Right-Click on Drawable select vector asset.
- Search for mail and select and rename it to ic_mail.xml.
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" android:tint="?attr/colorControlNormal"> <path android:fillColor="@android:color/white" android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/> </vector>
Ic_name.xml
- Right-Click on Drawable select vector asset
- Search for the name and select and rename it to ic_name.xml.
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" android:tint="?attr/colorControlNormal"> <path android:fillColor="@android:color/white" android:pathData="M12,5.9c1.16,0 2.1,0.94 2.1,2.1s-0.94,2.1 -2.1,2.1S9.9,9.16 9.9,8s0.94,-2.1 2.1,-2.1m0,9c2.97, 0 6.1,1.46 6.1,2.1v1.1L5.9,18.1L5.9,17c0,-0.64 3.13,-2.1 6.1,-2.1M12,4C9.79,4 8,5.79 8,8s1.79,4 4,4 4,-1.79 4,-4 -1.79,-4 -4,-4zM12,13c-2.67,0 -8,1.34 -8,4v3h16v-3c0,-2.66 -5.33,-4 -8,-4z"/> </vector>
Ic_phone.xml
- Right-Click on Drawable select vector asset
- Search for phone and select and rename it to ic_phone.xml.
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24" android:viewportHeight="24" android:tint="?attr/colorControlNormal"> <path android:fillColor="@android:color/white" android:pathData="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 0.67,-0.36 1.02,-0.24 1.12, 0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55 -0.45,1 -1,1 -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45, -1 1,-1h3.5c0.55,0 1,0.45 1,1 0,1.25 0.2,2.45 0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/> </vector>
Step 5: Creating Custom Toolbar
- Go to app -> res ->Layout
- Create a new resource file
- It is a custom toolbar
- we assign a text view to the toolbar
- And attach this toolbar to the Main activity.
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/background_main" android:elevation="4dp" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/SetTitle_main" android:text="Code Bun" android:textSize="25dp" android:layout_gravity="center" android:gravity="center" android:foregroundGravity="center" android:textStyle="bold" /> </androidx.appcompat.widget.Toolbar>
In this way, we create a login and registration in Android using the SQLite database.
Android practice tasks
https://codebun.com/login-and-registration-in-android-using-firebase/
https://codebun.com/dynamic-and-static-dropdown-menuspinner-in-android/
https://codebun.com/crud-operation-using-sqlite-in-android/
https://codebun.com/crud-operation-in-android-using-firebase-database/
https://codebun.com/how-to-get-data-from-api-in-android/
https://codebun.com/how-to-create-custom-alert-dialog-in-android/
https://codebun.com/search-and-sort-records-in-android-with-recycler-view/
https://codebun.com/create-a-custom-notification-with-custom-message-in-android/