We’ll look at how to make a simple Android registration and login application. This straightforward programme may be used in any system that requires authentication. Android is a Google-created mobile operating system. It’s found in a variety of devices, including smartphones, tablets, and even televisions. Developers interested in creating mobile apps may use Android because it is open source. It also has an adaptable framework that makes it easier for developers to create apps. So, let’s get to work on the code…
Getting Started
You’ll need to download and install the Android Development IDE first (Android Studio or Eclipse). Because Android Studio is an open source programming platform, you are free to create whatever you want.
The Android Studio may be found at https://developer.android.com/studio/index.html.
Design of the layout
Now we’ll make the application’s design. First, find the layout file named activity_main.xml, which is the default name when creating a new activity. Then, within your layout file, paste these codes.
<?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="com.razormist.simpleregistrationandloginapplication.MainActivity">
<EditText
android:id="@+id/et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10"
android:inputType="text"
android:hint="Username"/>
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10"
android:layout_below="@+id/et_username"
android:inputType="textPassword"
android:hint="Password"/>
<EditText
android:id="@+id/et_cpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10"
android:layout_below="@+id/et_password"
android:inputType="textPassword"
android:hint="Confirm Password"/>
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:ems="10"
android:text="Register"
android:layout_below="@+id/et_cpassword" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:ems="10"
android:text="Login"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Create a new empty activity named Login after that. It will also generate a new Login java file. Locate the new layout file activity_login.xml and paste the following code into it.
<?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="com.razormist.simpleregistrationandloginapplication.Login">
<EditText
android:id="@+id/et_lusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="145dp"
android:ems="10"
android:inputType="text"
android:hint="Username" />
<EditText
android:id="@+id/et_lpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_lusername"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:inputType="textPassword"
android:hint="Password" />
<Button
android:id="@+id/btn_llogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_lpassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:ems="10"
android:text="Login"/>
<Button
android:id="@+id/btn_lregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:text="Register"/>
</RelativeLayout>
Developing a Database
The script for building a database and a database connection is included in this code. To do so, first create a new java file named DatabaseHelper, then open it and extend the class by including SQLiteOpenHelper. Then, within the DatabaseHelper class, write these blocks of code.
public static final String DATABASE_NAME = "login.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE user(ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST user");
}
public boolean Insert(String username, String password){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("username", username);
contentValues.put("password", password);
long result = sqLiteDatabase.insert("user", null, contentValues);
if(result == -1){
return false;
}else{
return true;
}
}
public Boolean CheckUsername(String username){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM user WHERE username=?", new String[]{username});
if(cursor.getCount() > 0){
return false;
}else{
return true;
}
}
public Boolean CheckLogin(String username, String password){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM user WHERE username=? AND password=?", new String[]{username, password});
if(cursor.getCount() > 0){
return true;
}else{
return false;
}
}
Developing a Registration
The application’s register function is contained in this code. When the button is pressed, this will call the DatabaseHelper, which will enter the data into the database. Simply add these lines of code within the MainActivity class to do this.
package com.razormist.simpleregistrationandloginapplication;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
EditText et_username, et_password, et_cpassword;
Button btn_register, btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
et_username = (EditText)findViewById(R.id.et_username);
et_password = (EditText)findViewById(R.id.et_password);
et_cpassword = (EditText)findViewById(R.id.et_cpassword);
btn_register = (Button)findViewById(R.id.btn_register);
btn_login = (Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Login.class);
startActivity(intent);
}
});
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = et_username.getText().toString();
String password = et_password.getText().toString();
String confirm_password = et_cpassword.getText().toString();
if(username.equals("") || password.equals("") || confirm_password.equals("")){
Toast.makeText(getApplicationContext(), "Fields Required", Toast.LENGTH_SHORT).show();
}else{
if(password.equals(confirm_password)){
Boolean checkusername = databaseHelper.CheckUsername(username);
if(checkusername == true){
Boolean insert = databaseHelper.Insert(username, password);
if(insert == true){
Toast.makeText(getApplicationContext(), "Registered", Toast.LENGTH_SHORT).show();
et_username.setText("");
et_password.setText("");
et_cpassword.setText("");
}
}else{
Toast.makeText(getApplicationContext(), "Username already taken", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
Making the Login
This code includes the application’s login function. This code reads the data from the input field and checks if it exists in the DatabaseHelper class. Simply put these lines of code within the Login class to do this.
package com.razormist.simpleregistrationandloginapplication;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends AppCompatActivity {
Button btn_lregister, btn_llogin;
EditText et_lusername, et_lpassword;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
databaseHelper = new DatabaseHelper(this);
et_lusername = (EditText)findViewById(R.id.et_lusername);
et_lpassword = (EditText)findViewById(R.id.et_lpassword);
btn_llogin = (Button)findViewById(R.id.btn_llogin);
btn_lregister = (Button)findViewById(R.id.btn_lregister);
btn_lregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
}
});
btn_llogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = et_lusername.getText().toString();
String password = et_lpassword.getText().toString();
Boolean checklogin = databaseHelper.CheckLogin(username, password);
if(checklogin == true){
Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Done!