SQLite Database stores all the user data in the user’s device itself. It is an open-source database. In the file manager, you can check the database of the user which is in text file format.
In our project. we have created a login and signup page in android studio using java where we will save all the user information such as email and password in the SQLite database.
You can view the database either through SQLDatabase Software or by downloading the Simple SQLite Database Plugin in Android Studio where you can see database tables.
Step 1: Open Android Studio. Create New Project and choose Empty Activity.
Step 2: Pre-requisites
Gradle module
buildFeatures{ viewBinding true }
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="purple_200">#FFBB86FC</color> <color name="purple_500">#FF6200EE</color> <color name="purple_700">#FF3700B3</color> <color name="teal_200">#FF03DAC5</color> <color name="teal_700">#FF018786</color> <color name="black">#FF000000</color> <color name="white">#FFFFFFFF</color> <color name="lavender">#8692f7</color> </resources>
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools"> <!-- Base application theme. --> <style name="Theme.LogSignSQLPractice" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- Primary brand color. --> <item name="colorPrimary">@color/lavender</item> <item name="colorPrimaryVariant">@color/lavender</item> <item name="colorOnPrimary">@color/white</item> <!-- Secondary brand color. --> <item name="colorSecondary">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_700</item> <item name="colorOnSecondary">@color/black</item> <!-- Status bar color. --> <item name="android:statusBarColor">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> </style> </resources>
lavender_border.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="20dp"/> <stroke android:color="@color/lavender" android:width="2dp"/> <solid android:color="@color/white"/> </shape>
Drawables
Vector Assets – email, lock, and password.
Download Drawables
Step 3: XML Files
activity_signup.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView 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:background="@drawable/sqlsignbkg" tools:context=".SignupActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="380dp" android:padding="20dp"> <EditText android:layout_width="350dp" android:layout_height="60dp" android:id="@+id/signup_email" android:background="@drawable/lavender_border" android:layout_marginTop="30dp" android:padding="8dp" android:hint="Email" android:drawableLeft="@drawable/ic_baseline_email_24" android:drawablePadding="8dp"/> <EditText android:layout_width="350dp" android:layout_height="60dp" android:id="@+id/signup_password" android:background="@drawable/lavender_border" android:layout_marginTop="20dp" android:padding="8dp" android:hint="Password" android:inputType="textPassword" android:drawableLeft="@drawable/ic_baseline_lock_24" android:drawablePadding="8dp"/> <EditText android:layout_width="350dp" android:layout_height="60dp" android:id="@+id/signup_confirm" android:background="@drawable/lavender_border" android:layout_marginTop="20dp" android:padding="8dp" android:hint="Confirm Password" android:inputType="textPassword" android:drawableLeft="@drawable/ic_baseline_password_24" android:drawablePadding="8dp"/> <Button android:layout_width="350dp" android:layout_height="70dp" android:text="Sign Up" android:id="@+id/signup_button" android:textSize="18sp" android:layout_marginTop="30dp" app:cornerRadius = "30dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/loginRedirectText" android:text="Already an user? Login" android:layout_gravity="center" android:padding="8dp" android:textSize="18sp" android:layout_marginTop="8dp" android:textColor="@color/lavender"/> </LinearLayout> </LinearLayout> </ScrollView>
activity_login.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:background="@drawable/sqlogbkg" android:orientation="vertical" tools:context=".LoginActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="500dp" android:orientation="vertical" android:padding="20dp"> <EditText android:id="@+id/login_email" android:layout_width="350dp" android:layout_height="60dp" android:layout_marginTop="30dp" android:background="@drawable/lavender_border" android:drawableLeft="@drawable/ic_baseline_email_24" android:drawablePadding="8dp" android:hint="Email" android:padding="8dp"/> <EditText android:id="@+id/login_password" android:layout_width="350dp" android:layout_height="60dp" android:layout_marginTop="20dp" android:background="@drawable/lavender_border" android:drawableLeft="@drawable/ic_baseline_lock_24" android:drawablePadding="8dp" android:hint="Password" android:inputType="textPassword" android:padding="8dp" /> <Button android:id="@+id/login_button" android:layout_width="350dp" android:layout_height="70dp" android:layout_marginTop="30dp" android:text="Login" android:textSize="18sp" app:cornerRadius="30dp" /> <TextView android:id="@+id/signupRedirectText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="14dp" android:padding="8dp" android:text="Not yet registered? Signup" android:textColor="@color/lavender" android:textSize="18sp" /> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
activity_main.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=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Home" android:textSize="36sp" android:textColor="@color/lavender" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
Step 4: Java Files
DatabaseHelper.java
package com.example.logsignsqlpractice; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DatabaseHelper extends SQLiteOpenHelper { public static final String databaseName = "SignLog.db"; public DatabaseHelper(@Nullable Context context) { super(context, "SignLog.db", null, 1); } @Override public void onCreate(SQLiteDatabase MyDatabase) { MyDatabase.execSQL("create Table users(email TEXT primary key, password TEXT)"); } @Override public void onUpgrade(SQLiteDatabase MyDB, int i, int i1) { MyDB.execSQL("drop Table if exists users"); } public Boolean insertData(String email, String password){ SQLiteDatabase MyDatabase = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("email", email); contentValues.put("password", password); long result = MyDatabase.insert("users", null, contentValues); if (result == -1) { return false; } else { return true; } } public Boolean checkEmail(String email){ SQLiteDatabase MyDatabase = this.getWritableDatabase(); Cursor cursor = MyDatabase.rawQuery("Select * from users where email = ?", new String[]{email}); if(cursor.getCount() > 0) { return true; }else { return false; } } public Boolean checkEmailPassword(String email, String password){ SQLiteDatabase MyDatabase = this.getWritableDatabase(); Cursor cursor = MyDatabase.rawQuery("Select * from users where email = ? and password = ?", new String[]{email, password}); if (cursor.getCount() > 0) { return true; }else { return false; } } }
SignupActivity.java
package com.example.logsignsqlpractice; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.example.logsignsqlpractice.databinding.ActivitySignupBinding; public class SignupActivity extends AppCompatActivity { ActivitySignupBinding binding; DatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivitySignupBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); databaseHelper = new DatabaseHelper(this); binding.signupButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String email = binding.signupEmail.getText().toString(); String password = binding.signupPassword.getText().toString(); String confirmPassword = binding.signupConfirm.getText().toString(); if(email.equals("")||password.equals("")||confirmPassword.equals("")) Toast.makeText(SignupActivity.this, "All fields are mandatory", Toast.LENGTH_SHORT).show(); else{ if(password.equals(confirmPassword)){ Boolean checkUserEmail = databaseHelper.checkEmail(email); if(checkUserEmail == false){ Boolean insert = databaseHelper.insertData(email, password); if(insert == true){ Toast.makeText(SignupActivity.this, "Signup Successfully!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(getApplicationContext(),LoginActivity.class); startActivity(intent); }else{ Toast.makeText(SignupActivity.this, "Signup Failed!", Toast.LENGTH_SHORT).show(); } } else{ Toast.makeText(SignupActivity.this, "User already exists! Please login", Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(SignupActivity.this, "Invalid Password!", Toast.LENGTH_SHORT).show(); } } } }); binding.loginRedirectText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(SignupActivity.this, LoginActivity.class); startActivity(intent); } }); } }
LoginActivity.java
package com.example.logsignsqlpractice; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast; import com.example.logsignsqlpractice.databinding.ActivityLoginBinding; public class LoginActivity extends AppCompatActivity { ActivityLoginBinding binding; DatabaseHelper databaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityLoginBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); databaseHelper = new DatabaseHelper(this); binding.loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String email = binding.loginEmail.getText().toString(); String password = binding.loginPassword.getText().toString(); if(email.equals("")||password.equals("")) Toast.makeText(LoginActivity.this, "All fields are mandatory", Toast.LENGTH_SHORT).show(); else{ Boolean checkCredentials = databaseHelper.checkEmailPassword(email, password); if(checkCredentials == true){ Toast.makeText(LoginActivity.this, "Login Successfully!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(getApplicationContext(), MainActivity.class); startActivity(intent); }else{ Toast.makeText(LoginActivity.this, "Invalid Credentials", Toast.LENGTH_SHORT).show(); } } } }); binding.signupRedirectText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(LoginActivity.this, SignupActivity.class); startActivity(intent); } }); } }
If you find this article easy and insightful, please share it with your friends.
If you have any queries or errors related to the above context, please feel free to reach out through the comment section.
For a detailed explanation, please check my YouTube Video: Login and Signup using SQLite in Android Studio using Java
Check my other articles on Android Knowledge: https://androidknowledge.com/android/
Thank you 🙌