In this tutorial, we will try to create a Simple Registration and Login Application using Android. This simple application can be used in any system that needed a login verification. Android is a mobile operating system developed by Google. It used in several gadgets like smartphones, tablets, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provides an adaptive framework that allows the developer to develop an app in a simpler way. So let's now do the coding...
First, you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.
Here's the link for the Android Studio https://developer.android.com/studio/index.html.
We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside 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"/> 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" /> 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>
<?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" /> 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"/> 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>
The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.
<?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>
This code contain the script for creating a database and a database connection. To create this first create a new java file called DatabaseHelper, open the newly created file then extend the class by adding SQLiteOpenHelper. After that write these block of codes inside the DatabaseHelper class.
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"); } 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; } } SQLiteDatabase sqLiteDatabase = this.getWritableDatabase(); if(cursor.getCount() > 0){ return false; }else{ return true; } } SQLiteDatabase sqLiteDatabase = this.getReadableDatabase(); if(cursor.getCount() > 0){ return true; }else{ return false; } }
This code contains the register function of the application. This will insert the data to the database by calling the DatabaseHelper when the button is clicked. To do that just write these block of codes inside the MainActivity class.
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); @Override Intent intent = new Intent(MainActivity.this, Login.class); startActivity(intent); } }); @Override if(username.equals("") || password.equals("") || confirm_password.equals("")){ Toast.makeText(getApplicationContext(), "Fields Required", Toast.LENGTH_SHORT).show(); }else{ if(password.equals(confirm_password)){ if(checkusername == true){ 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(); } } } }); } }
This code contains the login function for the application. This code will read the entry field data then check the data if it exist in the DatabaseHelper class. To do that simply write these block of codes inside the Login class.
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); @Override Intent intent = new Intent(Login.this, MainActivity.class); startActivity(intent); } }); @Override if(checklogin == true){ Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show(); } } }); } }
Try to run the app and see if it worked.
There you have it we have created a Simple Registration and Login Application using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site.
Enjoy Coding!!!
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.