Create Android Registration and Login Application Tutorial with Free Source Code

Posts2

In this post you will learn about Create Android Registration and Login Application Tutorial with Free Source Code. Read this below article carefully and understand this to put impact in real life.

Operating System

In this article, we’ll attempt to use Android to build a simple registration and login application. Any system that requires login verification can utilize this straightforward program. Google created the smartphone operating program called Android. It is utilized in many devices, including televisions, tablets, and smartphones. Android is available as an open source for developers who are interested in creating mobile applications. Additionally, it offers a flexible architecture that makes it easier for the developer to create an app. Let’s begin the coding process now.

Getting Started:

First of all, you should have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open-source development tool that helps you to continuos the next step.

Here you can Download Android Studio Tools.

Layout Design

Now that the design for the application is complete, find the layout file called activity main.xml, which is the name that appears by default when a new activity is created. After that, add these scripts to your layout file.

<?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>

Next, create a brand-new, blank activity called Login. Additionally, a new Java file called Login will be created. Write the following code in the newly created layout file 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="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>

Android Manifest File

The Android Manifest file gives the Android system crucial details about your app, which the system needs before launching the code. It provides an overview of the application’s information. It includes some libraries required to access the various app methods.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.razormist.simpleregistrationandloginapplication">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Login"
            android:configChanges="orientation"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="com.razormist.simpleregistrationandloginapplication.Login" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

MakingThe Database

The script for creating a database and a database connection is contained in this code. To do this, first create a new Java file called DatabaseHelper, then open it and add SQLiteOpenHelper to the class extension. Write this code block within the DatabaseHelper class after that.

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;
        }
    }

Making the Registration

The application’s register function is contained in this code. When the button is pressed, this will call the DatabaseHelper to enter the data into the database. Simply insert these code blocks into the MainActivity class to accomplish that.

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

The application’s login functionality is contained in this code. This program reads the data from the entry field and then determines whether it is present in the database helper class. Simply insert these blocks of code into the Login class to accomplish that.

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();
                }
            }
        });
    }
 
}

So there you have it—we used Android to construct a straightforward registration and login application. I hope this guide will enable you to find what you’re looking for. Please visit this website for further information and tutorials.

Download

Final Words

In this post you learnt about Create Android Registration and Login Application Tutorial with Free Source Code. If you like this article please share with your friends and family to support our website. We wrote this article by researching on internet to find best content for you. You can find more articles like this on our website for free. We provided you some important tips regarding this topic by following them you can easily understand. If you need more information regarding Create Android Registration and Login Application Tutorial with Free Source Code you can ask in comments or you can contact us via contact us form available on this website. Thanks for reading and sharing.


Leave a Reply

Your email address will not be published. Required fields are marked *