Android Simple Registration and Login Application Tutorial with Source Code

Operating System

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

Getting Started:

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.

Layout Design

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.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simpleregistrationandloginapplication.MainActivity">
  8.  
  9.  
  10. <EditText
  11. android:id="@+id/et_username"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerHorizontal="true"
  16. android:layout_marginTop="65dp"
  17. android:ems="10"
  18. android:inputType="text"
  19. android:hint="Username"/>
  20.  
  21.  
  22. <EditText
  23. android:id="@+id/et_password"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_centerHorizontal="true"
  27. android:layout_marginTop="65dp"
  28. android:ems="10"
  29. android:layout_below="@+id/et_username"
  30. android:inputType="textPassword"
  31. android:hint="Password"/>
  32.  
  33. <EditText
  34. android:id="@+id/et_cpassword"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_centerHorizontal="true"
  38. android:layout_marginTop="65dp"
  39. android:ems="10"
  40. android:layout_below="@+id/et_password"
  41. android:inputType="textPassword"
  42. android:hint="Confirm Password"/>
  43.  
  44. android:id="@+id/btn_register"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:layout_centerHorizontal="true"
  48. android:layout_marginTop="65dp"
  49. android:ems="10"
  50. android:text="Register"
  51. android:layout_below="@+id/et_cpassword" />
  52.  
  53. android:id="@+id/btn_login"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:layout_centerHorizontal="true"
  57. android:ems="10"
  58. android:text="Login"
  59. android:layout_alignParentBottom="true"/>
  60.  
  61.  
  62. </RelativeLayout>
Then after that create a new empty activity called Login. It will also created a new java file called Login. Locate the new layout file called activity_login.xml and write these code inside the file.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simpleregistrationandloginapplication.Login">
  8.  
  9. <EditText
  10. android:id="@+id/et_lusername"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentTop="true"
  14. android:layout_centerHorizontal="true"
  15. android:layout_marginTop="145dp"
  16. android:ems="10"
  17. android:inputType="text"
  18. android:hint="Username" />
  19.  
  20. <EditText
  21. android:id="@+id/et_lpassword"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_below="@id/et_lusername"
  25. android:layout_centerHorizontal="true"
  26. android:layout_marginTop="50dp"
  27. android:ems="10"
  28. android:inputType="textPassword"
  29. android:hint="Password" />
  30.  
  31. android:id="@+id/btn_llogin"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_below="@id/et_lpassword"
  35. android:layout_centerHorizontal="true"
  36. android:layout_marginTop="50dp"
  37. android:ems="10"
  38. android:text="Login"/>
  39.  
  40. android:id="@+id/btn_lregister"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content"
  43. android:layout_alignParentBottom="true"
  44. android:layout_centerHorizontal="true"
  45. android:ems="10"
  46. android:text="Register"/>
  47.  
  48.  
  49. </RelativeLayout>

Android Manifest File

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.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simpleregistrationandloginapplication">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity
  13. android:name=".Login"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. <activity
  22. android:name=".MainActivity"
  23. android:configChanges="orientation"
  24. android:label="@string/app_name"
  25. android:theme="@style/AppTheme">
  26. <intent-filter>
  27. <action android:name="com.razormist.simpleregistrationandloginapplication.Login" />
  28. <category android:name="android.intent.category.LAUNCHER" />
  29. </intent-filter>
  30. </activity>
  31. </application>
  32.  
  33. </manifest>

Creating The Database

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.

  1. public static final String DATABASE_NAME = "login.db";
  2.  
  3.  
  4. public DatabaseHelper(Context context) {
  5. super(context, DATABASE_NAME, null, 1);
  6. }
  7.  
  8. @Override
  9. public void onCreate(SQLiteDatabase db) {
  10. db.execSQL("CREATE TABLE user(ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT)");
  11. }
  12.  
  13. @Override
  14. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  15. db.execSQL("DROP TABLE IF EXIST user");
  16. }
  17.  
  18. public boolean Insert(String username, String password){
  19. SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
  20. ContentValues contentValues = new ContentValues();
  21. contentValues.put("username", username);
  22. contentValues.put("password", password);
  23. long result = sqLiteDatabase.insert("user", null, contentValues);
  24. if(result == -1){
  25. return false;
  26. }else{
  27. return true;
  28. }
  29. }
  30.  
  31. public Boolean CheckUsername(String username){
  32. SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
  33. Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM user WHERE username=?", new String[]{username});
  34. if(cursor.getCount() > 0){
  35. return false;
  36. }else{
  37. return true;
  38. }
  39. }
  40.  
  41. public Boolean CheckLogin(String username, String password){
  42. SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
  43. Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM user WHERE username=? AND password=?", new String[]{username, password});
  44. if(cursor.getCount() > 0){
  45. return true;
  46. }else{
  47. return false;
  48. }
  49. }

Creating the Registration

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.

  1. package com.razormist.simpleregistrationandloginapplication;
  2.  
  3. import android.content.Intent;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12. DatabaseHelper databaseHelper;
  13.  
  14. EditText et_username, et_password, et_cpassword;
  15. Button btn_register, btn_login;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21.  
  22. databaseHelper = new DatabaseHelper(this);
  23. et_username = (EditText)findViewById(R.id.et_username);
  24. et_password = (EditText)findViewById(R.id.et_password);
  25. et_cpassword = (EditText)findViewById(R.id.et_cpassword);
  26. btn_register = (Button)findViewById(R.id.btn_register);
  27. btn_login = (Button)findViewById(R.id.btn_login);
  28.  
  29. btn_login.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. Intent intent = new Intent(MainActivity.this, Login.class);
  33. startActivity(intent);
  34. }
  35. });
  36.  
  37. btn_register.setOnClickListener(new View.OnClickListener() {
  38. @Override
  39. public void onClick(View v) {
  40. String username = et_username.getText().toString();
  41. String password = et_password.getText().toString();
  42. String confirm_password = et_cpassword.getText().toString();
  43.  
  44. if(username.equals("") || password.equals("") || confirm_password.equals("")){
  45. Toast.makeText(getApplicationContext(), "Fields Required", Toast.LENGTH_SHORT).show();
  46. }else{
  47. if(password.equals(confirm_password)){
  48. Boolean checkusername = databaseHelper.CheckUsername(username);
  49. if(checkusername == true){
  50. Boolean insert = databaseHelper.Insert(username, password);
  51. if(insert == true){
  52. Toast.makeText(getApplicationContext(), "Registered", Toast.LENGTH_SHORT).show();
  53. et_username.setText("");
  54. et_password.setText("");
  55. et_cpassword.setText("");
  56. }
  57. }else{
  58. Toast.makeText(getApplicationContext(), "Username already taken", Toast.LENGTH_SHORT).show();
  59. }
  60. }else{
  61. Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_SHORT).show();
  62. }
  63. }
  64. }
  65. });
  66. }
  67. }

Creating the Login

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.

  1. package com.razormist.simpleregistrationandloginapplication;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11.  
  12. public class Login extends AppCompatActivity {
  13. Button btn_lregister, btn_llogin;
  14. EditText et_lusername, et_lpassword;
  15.  
  16. DatabaseHelper databaseHelper;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_login);
  22.  
  23. databaseHelper = new DatabaseHelper(this);
  24.  
  25. et_lusername = (EditText)findViewById(R.id.et_lusername);
  26. et_lpassword = (EditText)findViewById(R.id.et_lpassword);
  27.  
  28. btn_llogin = (Button)findViewById(R.id.btn_llogin);
  29. btn_lregister = (Button)findViewById(R.id.btn_lregister);
  30.  
  31. btn_lregister.setOnClickListener(new View.OnClickListener() {
  32. @Override
  33. public void onClick(View v) {
  34. Intent intent = new Intent(Login.this, MainActivity.class);
  35. startActivity(intent);
  36. }
  37. });
  38.  
  39. btn_llogin.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. String username = et_lusername.getText().toString();
  43. String password = et_lpassword.getText().toString();
  44.  
  45. Boolean checklogin = databaseHelper.CheckLogin(username, password);
  46. if(checklogin == true){
  47. Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_SHORT).show();
  48. }else{
  49. Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
  50. }
  51. }
  52. });
  53. }
  54.  
  55. }

Try to run the app and see if it worked.

DEMO

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.

Submitted byAnonymous (not verified)on Tue, 08/17/2021 - 21:18

Hii.. Thanks that's a great project but please let me know how can i see my user input data I'll be very glad if you do that..

Add new comment