Android - Simple Registration and Login Application

Submitted by: 
Operating System: 
Visitors have accessed this post 11931 times.

In this tutorial we will try to create a Simple Registration and Login Application using Android. This simple application can used to any system that needed a login verification. Android is a mobile operating system developed by Google. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provide an adaptive framework that allow the developer to develop an apps 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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simpleregistrationandloginapplication.MainActivity">
  11.  
  12.  
  13. <EditText
  14. android:id="@+id/et_username"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentTop="true"
  18. android:layout_centerHorizontal="true"
  19. android:layout_marginTop="65dp"
  20. android:ems="10"
  21. android:inputType="text"
  22. android:hint="Username"/>
  23.  
  24.  
  25. <EditText
  26. android:id="@+id/et_password"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_centerHorizontal="true"
  30. android:layout_marginTop="65dp"
  31. android:ems="10"
  32. android:layout_below="@+id/et_username"
  33. android:inputType="textPassword"
  34. android:hint="Password"/>
  35.  
  36. <EditText
  37. android:id="@+id/et_cpassword"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_centerHorizontal="true"
  41. android:layout_marginTop="65dp"
  42. android:ems="10"
  43. android:layout_below="@+id/et_password"
  44. android:inputType="textPassword"
  45. android:hint="Confirm Password"/>
  46.  
  47. android:id="@+id/btn_register"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_centerHorizontal="true"
  51. android:layout_marginTop="65dp"
  52. android:ems="10"
  53. android:text="Register"
  54. android:layout_below="@+id/et_cpassword" />
  55.  
  56. android:id="@+id/btn_login"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_centerHorizontal="true"
  60. android:ems="10"
  61. android:text="Login"
  62. android:layout_alignParentBottom="true"/>
  63.  
  64.  
  65. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simpleregistrationandloginapplication.Login">
  11.  
  12. <EditText
  13. android:id="@+id/et_lusername"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_alignParentTop="true"
  17. android:layout_centerHorizontal="true"
  18. android:layout_marginTop="145dp"
  19. android:ems="10"
  20. android:inputType="text"
  21. android:hint="Username" />
  22.  
  23. <EditText
  24. android:id="@+id/et_lpassword"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:layout_below="@id/et_lusername"
  28. android:layout_centerHorizontal="true"
  29. android:layout_marginTop="50dp"
  30. android:ems="10"
  31. android:inputType="textPassword"
  32. android:hint="Password" />
  33.  
  34. android:id="@+id/btn_llogin"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_below="@id/et_lpassword"
  38. android:layout_centerHorizontal="true"
  39. android:layout_marginTop="50dp"
  40. android:ems="10"
  41. android:text="Login"/>
  42.  
  43. android:id="@+id/btn_lregister"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_alignParentBottom="true"
  47. android:layout_centerHorizontal="true"
  48. android:ems="10"
  49. android:text="Register"/>
  50.  
  51.  
  52. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simpleregistrationandloginapplication">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity
  14. android:name=".Login"
  15. android:configChanges="orientation"
  16. android:screenOrientation="portrait">
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity
  23. android:name=".MainActivity"
  24. android:configChanges="orientation"
  25. android:label="@string/app_name"
  26. android:theme="@style/AppTheme">
  27. <intent-filter>
  28. <action android:name="com.razormist.simpleregistrationandloginapplication.Login" />
  29. <category android:name="android.intent.category.LAUNCHER" />
  30. </intent-filter>
  31. </activity>
  32. </application>
  33.  
  34. </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 android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13. DatabaseHelper databaseHelper;
  14.  
  15. EditText et_username, et_password, et_cpassword;
  16. Button btn_register, btn_login;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22.  
  23. databaseHelper = new DatabaseHelper(this);
  24. et_username = (EditText)findViewById(R.id.et_username);
  25. et_password = (EditText)findViewById(R.id.et_password);
  26. et_cpassword = (EditText)findViewById(R.id.et_cpassword);
  27. btn_register = (Button)findViewById(R.id.btn_register);
  28. btn_login = (Button)findViewById(R.id.btn_login);
  29.  
  30. btn_login.setOnClickListener(new View.OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. Intent intent = new Intent(MainActivity.this, Login.class);
  34. startActivity(intent);
  35. }
  36. });
  37.  
  38. btn_register.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View v) {
  41. String username = et_username.getText().toString();
  42. String password = et_password.getText().toString();
  43. String confirm_password = et_cpassword.getText().toString();
  44.  
  45. if(username.equals("") || password.equals("") || confirm_password.equals("")){
  46. Toast.makeText(getApplicationContext(), "Fields Required", Toast.LENGTH_SHORT).show();
  47. }else{
  48. if(password.equals(confirm_password)){
  49. Boolean checkusername = databaseHelper.CheckUsername(username);
  50. if(checkusername == true){
  51. Boolean insert = databaseHelper.Insert(username, password);
  52. if(insert == true){
  53. Toast.makeText(getApplicationContext(), "Registered", Toast.LENGTH_SHORT).show();
  54. et_username.setText("");
  55. et_password.setText("");
  56. et_cpassword.setText("");
  57. }
  58. }else{
  59. Toast.makeText(getApplicationContext(), "Username already taken", Toast.LENGTH_SHORT).show();
  60. }
  61. }else{
  62. Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_SHORT).show();
  63. }
  64. }
  65. }
  66. });
  67. }
  68. }

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 android.support.v7.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.

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.