How to create login and user registration form in android studio using SQLite database?
Simple User Registration and Login Form in Android studio.
In this blog post, create a simple user registration and login form using an SQLite database in Android Studio.
We will also use authentication for invalid passwords or display an error message if the user already has an account.
We will use the SQLite database to store usernames and passwords in the database.
Also, we will use material design in the edit text box and button.
First, we will create a database OpenHelper Class namedDBmain.java
package com.pd.userlogin; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DBmain extends SQLiteOpenHelper { public DBmain(@Nullable Context context) { super(context, "LoginDb", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table users(username text primary key, password text)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists users"); } }
MainActivity.java
package com.pd.userlogin; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.android.material.textfield.TextInputEditText; public class MainActivity extends AppCompatActivity { TextInputEditText username,password,repassword; Button registration; TextView loginform; DBmain dBmain; SQLiteDatabase sqLiteDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); username=(TextInputEditText)findViewById(R.id.username); password=(TextInputEditText)findViewById(R.id.password); repassword=(TextInputEditText)findViewById(R.id.repassword); loginform=(TextView)findViewById(R.id.login_form); registration=(Button)findViewById(R.id.btnregi); dBmain=new DBmain(this); registration.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String user=username.getText().toString(); String pass=password.getText().toString(); String repass=repassword.getText().toString(); if (username.getText().toString().length()<=0){ username.setError("Please set username"); }else if (password.getText().toString().length()<=0){ password.setError("Please set Password"); }else if (repassword.getText().toString().length()<=0){ repassword.setError("Please set Repassword"); }else if (username.equals("")||password.equals("")||repassword.equals("")){ Toast.makeText(MainActivity.this, "Empty field not allow", Toast.LENGTH_SHORT).show(); }else{ if (pass.equals(repass)){ Boolean checkuser=checkusername(user); if (checkuser==false){ Boolean insert=inserData(user,pass); if (insert==true){ Toast.makeText(MainActivity.this, "Registration successfully", Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(),LoginForm.class)); }else { Toast.makeText(MainActivity.this, "Registration failure", Toast.LENGTH_SHORT).show(); } }else { Toast.makeText(MainActivity.this, "user name already exists", Toast.LENGTH_SHORT).show(); } }else{ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("Try Again"); builder.setIcon(R.drawable.ic_baseline_error_24); builder.setMessage("Password not Match"); builder.show(); } } } }); loginform.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getApplicationContext(),LoginForm.class)); } }); } private Boolean inserData(String user, String pass) { sqLiteDatabase=dBmain.getWritableDatabase(); ContentValues contentValues=new ContentValues(); contentValues.put("username",user); contentValues.put("password",pass); long rec=sqLiteDatabase.insert("users",null,contentValues); if (rec==-1){ return false; }else { return true; } } private Boolean checkusername(String user) { sqLiteDatabase=dBmain.getWritableDatabase(); Cursor cursor=sqLiteDatabase.rawQuery("select *from users where username=?",new String[]{user}); if (cursor.getCount()>0){ return true; }else { return false; } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center_vertical" android:orientation="vertical" android:background="@drawable/bgcolor" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:gravity="center" android:text="User Login form" android:textSize="25dp" /> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:hint="Enter username" app:endIconMode="clear_text"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:hint="Enter Password"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:hint=" ReEnter Password"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/repassword" android:layout_width="match_parent" android:layout_height="match_parent" android:ems="25" android:inputType="textPassword" /> </com.google.android.material.textfield.TextInputLayout> <Button android:id="@+id/btnregi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:text="Registration" /> <TextView android:id="@+id/login_form" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:text="Already have a account" /> </LinearLayout>
LoginForm.java
package com.pd.userlogin; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.google.android.material.textfield.TextInputEditText; public class LoginForm extends AppCompatActivity { TextInputEditText loginuser,loginpassword; Button btnlogin; DBmain dBmain; SQLiteDatabase sqLiteDatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_form); loginuser=findViewById(R.id.login_user); loginpassword=findViewById(R.id.login_password); btnlogin=findViewById(R.id.btn_login); dBmain=new DBmain(this); btnlogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String user=loginuser.getText().toString(); String pass=loginpassword.getText().toString(); if (user.equals("")||pass.equals("")){ Toast.makeText(LoginForm.this, "Empty field Not allow", Toast.LENGTH_SHORT).show(); }else { Boolean checkuserpass=checkuserpassword(user,pass); if (checkuserpass==true){ Toast.makeText(LoginForm.this, "Login successfully", Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(),Welcome.class)); }else { AlertDialog.Builder builder=new AlertDialog.Builder(LoginForm.this); builder.setTitle("Error Messsage"); builder.setMessage("Password and Username are Wrong"); builder.setIcon(R.drawable.ic_baseline_error_24); builder.setPositiveButton("OK",null); builder.setCancelable(true); final AlertDialog alertDialog= builder.create(); alertDialog.show(); alertDialog.getWindow().setGravity(Gravity.TOP); } } } }); } private Boolean checkuserpassword(String user, String pass) { sqLiteDatabase=dBmain.getWritableDatabase(); Cursor cursor=sqLiteDatabase.rawQuery("select *from users where username=? and password=?",new String[]{user,pass}); if (cursor.getCount()>0){ return true; }else{ return false; } } public void Regi(View view) { startActivity(new Intent(getApplicationContext(),MainActivity.class)); } }
activity_login_form.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:gravity="center_vertical" android:background="@drawable/bgcolor" tools:context=".LoginForm"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Login" android:layout_marginRight="@dimen/mr" android:layout_marginLeft="@dimen/ml" android:gravity="center" android:layout_gravity="center" android:textSize="25dp" android:textStyle="bold"/> <com.google.android.material.textfield.TextInputLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:hint="username" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" app:endIconMode="clear_text"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/login_user" android:ems="25"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/ml" android:layout_marginRight="@dimen/mr" android:hint="Enter Password" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" app:endIconMode="password_toggle"> <com.google.android.material.textfield.TextInputEditText android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/login_password" android:ems="25"/> </com.google.android.material.textfield.TextInputLayout> <!-- <com.google.android.material.textfield.TextInputLayout--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:layout_marginLeft="@dimen/ml"--> <!-- android:layout_marginRight="@dimen/mr"--> <!-- android:hint="ReEnter Password"--> <!-- style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"--> <!-- app:endIconMode="password_toggle">--> <!-- <com.google.android.material.textfield.TextInputEditText--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:id="@+id/login_repassword"--> <!-- android:ems="25"/>--> <!-- </com.google.android.material.textfield.TextInputLayout>--> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="@dimen/mr" android:layout_marginLeft="@dimen/ml" android:id="@+id/btn_login" android:text="Login"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New User" android:layout_marginRight="@dimen/mr" android:onClick="Regi" android:layout_gravity="end"/> </LinearLayout>
Welcome.java
package com.pd.userlogin; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class Welcome extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); } }
activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".Welcome"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to Course Code" android:textSize="30dp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
https://youtu.be/PXUj0rIleQg
Tags:
User login app android