banner



How To Create Registration Page In Android Studio

Android Simple Registration and Login Application Tutorial with Source Code

Submitted by razormist on Monday, January 4, 2021 - 16:54.

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

  9. android:id= "@+id/et_username"

  10. android:layout_width= "wrap_content"

  11. android:layout_height= "wrap_content"

  12. android:layout_alignParentTop= "true"

  13. android:layout_centerHorizontal= "true"

  14. android:layout_marginTop= "65dp"

  15. android:ems= "10"

  16. android:inputType= "text"

  17. android:hint= "Username" />

  18. <EditText

  19. android:id= "@+id/et_password"

  20. android:layout_width= "wrap_content"

  21. android:layout_height= "wrap_content"

  22. android:layout_centerHorizontal= "true"

  23. android:layout_marginTop= "65dp"

  24. android:ems= "10"

  25. android:layout_below= "@+id/et_username"

  26. android:inputType= "textPassword"

  27. android:hint= "Password" />

  28. <EditText

  29. android:id= "@+id/et_cpassword"

  30. android:layout_width= "wrap_content"

  31. android:layout_height= "wrap_content"

  32. android:layout_centerHorizontal= "true"

  33. android:layout_marginTop= "65dp"

  34. android:ems= "10"

  35. android:layout_below= "@+id/et_password"

  36. android:inputType= "textPassword"

  37. android:hint= "Confirm Password" />

  38. android:id= "@+id/btn_register"

  39. android:layout_width= "wrap_content"

  40. android:layout_height= "wrap_content"

  41. android:layout_centerHorizontal= "true"

  42. android:layout_marginTop= "65dp"

  43. android:ems= "10"

  44. android:text= "Register"

  45. android:layout_below= "@+id/et_cpassword" />

  46. android:id= "@+id/btn_login"

  47. android:layout_width= "wrap_content"

  48. android:layout_height= "wrap_content"

  49. android:layout_centerHorizontal= "true"

  50. android:ems= "10"

  51. android:text= "Login"

  52. android:layout_alignParentBottom= "true" />

  53. </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. <EditText

  9. android:id= "@+id/et_lusername"

  10. android:layout_width= "wrap_content"

  11. android:layout_height= "wrap_content"

  12. android:layout_alignParentTop= "true"

  13. android:layout_centerHorizontal= "true"

  14. android:layout_marginTop= "145dp"

  15. android:ems= "10"

  16. android:inputType= "text"

  17. android:hint= "Username" />

  18. <EditText

  19. android:id= "@+id/et_lpassword"

  20. android:layout_width= "wrap_content"

  21. android:layout_height= "wrap_content"

  22. android:layout_below= "@id/et_lusername"

  23. android:layout_centerHorizontal= "true"

  24. android:layout_marginTop= "50dp"

  25. android:ems= "10"

  26. android:inputType= "textPassword"

  27. android:hint= "Password" />

  28. android:id= "@+id/btn_llogin"

  29. android:layout_width= "wrap_content"

  30. android:layout_height= "wrap_content"

  31. android:layout_below= "@id/et_lpassword"

  32. android:layout_centerHorizontal= "true"

  33. android:layout_marginTop= "50dp"

  34. android:ems= "10"

  35. android:text= "Login" />

  36. android:id= "@+id/btn_lregister"

  37. android:layout_width= "wrap_content"

  38. android:layout_height= "wrap_content"

  39. android:layout_alignParentBottom= "true"

  40. android:layout_centerHorizontal= "true"

  41. android:ems= "10"

  42. android:text= "Register" />

  43. </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. <application

  5. android:allowBackup= "true"

  6. android:icon= "@mipmap/ic_launcher"

  7. android:label= "@string/app_name"

  8. android:roundIcon= "@mipmap/ic_launcher_round"

  9. android:supportsRtl= "true"

  10. android:theme= "@style/AppTheme" >

  11. <activity

  12. android:name= ".Login"

  13. android:configChanges= "orientation"

  14. android:screenOrientation= "portrait" >

  15. <intent-filter>

  16. <action android:name= "android.intent.action.MAIN" />

  17. <category android:name= "android.intent.category.LAUNCHER" />

  18. </intent-filter>

  19. </activity>

  20. <activity

  21. android:name= ".MainActivity"

  22. android:configChanges= "orientation"

  23. android:label= "@string/app_name"

  24. android:theme= "@style/AppTheme" >

  25. <intent-filter>

  26. <action android:name= "com.razormist.simpleregistrationandloginapplication.Login" />

  27. <category android:name= "android.intent.category.LAUNCHER" />

  28. </intent-filter>

  29. </activity>

  30. </application>

  31. </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. public DatabaseHelper( Context context) {

  3. super (context, DATABASE_NAME, null, 1 ) ;

  4. }

  5. @Override

  6. public void onCreate(SQLiteDatabase db) {

  7. db.execSQL ( "CREATE TABLE user(ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT, password TEXT)" ) ;

  8. }

  9. @Override

  10. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  11. db.execSQL ( "DROP TABLE IF EXIST user" ) ;

  12. }

  13. public boolean Insert( String username, String password) {

  14. SQLiteDatabase sqLiteDatabase = this.getWritableDatabase ( ) ;

  15. ContentValues contentValues = new ContentValues( ) ;

  16. contentValues.put ( "username", username) ;

  17. contentValues.put ( "password", password) ;

  18. long result = sqLiteDatabase.insert ( "user", null, contentValues) ;

  19. if (result == - 1 ) {

  20. return false ;

  21. } else {

  22. return true ;

  23. }

  24. }

  25. SQLiteDatabase sqLiteDatabase = this.getWritableDatabase ( ) ;

  26. Cursor cursor = sqLiteDatabase.rawQuery ( "SELECT * FROM user WHERE username=?", new String [ ] {username} ) ;

  27. if (cursor.getCount ( ) > 0 ) {

  28. return false ;

  29. } else {

  30. return true ;

  31. }

  32. }

  33. SQLiteDatabase sqLiteDatabase = this.getReadableDatabase ( ) ;

  34. Cursor cursor = sqLiteDatabase.rawQuery ( "SELECT * FROM user WHERE username=? AND password=?", new String [ ] {username, password} ) ;

  35. if (cursor.getCount ( ) > 0 ) {

  36. return true ;

  37. } else {

  38. return false ;

  39. }

  40. }

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. import android.content.Intent ;

  3. import androidx.appcompat.app.AppCompatActivity ;

  4. import android.os.Bundle ;

  5. import android.view.View ;

  6. import android.widget.Button ;

  7. import android.widget.EditText ;

  8. import android.widget.Toast ;

  9. public class MainActivity extends AppCompatActivity {

  10. DatabaseHelper databaseHelper;

  11. EditText et_username, et_password, et_cpassword;

  12. Button btn_register, btn_login;

  13. @Override

  14. protected void onCreate(Bundle savedInstanceState) {

  15. super.onCreate (savedInstanceState) ;

  16. setContentView(R.layout.activity_main ) ;

  17. databaseHelper = new DatabaseHelper( this ) ;

  18. et_username = (EditText)findViewById(R.id.et_username ) ;

  19. et_password = (EditText)findViewById(R.id.et_password ) ;

  20. et_cpassword = (EditText)findViewById(R.id.et_cpassword ) ;

  21. btn_register = ( Button )findViewById(R.id.btn_register ) ;

  22. btn_login = ( Button )findViewById(R.id.btn_login ) ;

  23. btn_login.setOnClickListener ( new View.OnClickListener ( ) {

  24. @Override

  25. public void onClick( View v) {

  26. Intent intent = new Intent(MainActivity.this, Login.class ) ;

  27. startActivity(intent) ;

  28. }

  29. } ) ;

  30. btn_register.setOnClickListener ( new View.OnClickListener ( ) {

  31. @Override

  32. public void onClick( View v) {

  33. String username = et_username.getText ( ).toString ( ) ;

  34. String password = et_password.getText ( ).toString ( ) ;

  35. String confirm_password = et_cpassword.getText ( ).toString ( ) ;

  36. if (username.equals ( "" ) || password.equals ( "" ) || confirm_password.equals ( "" ) ) {

  37. Toast.makeText (getApplicationContext( ), "Fields Required", Toast.LENGTH_SHORT ).show ( ) ;

  38. } else {

  39. if (password.equals (confirm_password) ) {

  40. Boolean checkusername = databaseHelper.CheckUsername (username) ;

  41. if (checkusername == true ) {

  42. Boolean insert = databaseHelper.Insert (username, password) ;

  43. if (insert == true ) {

  44. Toast.makeText (getApplicationContext( ), "Registered", Toast.LENGTH_SHORT ).show ( ) ;

  45. et_username.setText ( "" ) ;

  46. et_password.setText ( "" ) ;

  47. et_cpassword.setText ( "" ) ;

  48. }

  49. } else {

  50. Toast.makeText (getApplicationContext( ), "Username already taken", Toast.LENGTH_SHORT ).show ( ) ;

  51. }

  52. } else {

  53. Toast.makeText (getApplicationContext( ), "Password does not match", Toast.LENGTH_SHORT ).show ( ) ;

  54. }

  55. }

  56. }

  57. } ) ;

  58. }

  59. }

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. import android.content.Intent ;

  3. import android.os.Bundle ;

  4. import androidx.appcompat.app.AppCompatActivity ;

  5. import android.view.View ;

  6. import android.widget.Button ;

  7. import android.widget.EditText ;

  8. import android.widget.Toast ;

  9. public class Login extends AppCompatActivity {

  10. Button btn_lregister, btn_llogin;

  11. EditText et_lusername, et_lpassword;

  12. DatabaseHelper databaseHelper;

  13. @Override

  14. protected void onCreate(Bundle savedInstanceState) {

  15. super.onCreate (savedInstanceState) ;

  16. setContentView(R.layout.activity_login ) ;

  17. databaseHelper = new DatabaseHelper( this ) ;

  18. et_lusername = (EditText)findViewById(R.id.et_lusername ) ;

  19. et_lpassword = (EditText)findViewById(R.id.et_lpassword ) ;

  20. btn_llogin = ( Button )findViewById(R.id.btn_llogin ) ;

  21. btn_lregister = ( Button )findViewById(R.id.btn_lregister ) ;

  22. btn_lregister.setOnClickListener ( new View.OnClickListener ( ) {

  23. @Override

  24. public void onClick( View v) {

  25. Intent intent = new Intent(Login.this, MainActivity.class ) ;

  26. startActivity(intent) ;

  27. }

  28. } ) ;

  29. btn_llogin.setOnClickListener ( new View.OnClickListener ( ) {

  30. @Override

  31. public void onClick( View v) {

  32. String username = et_lusername.getText ( ).toString ( ) ;

  33. String password = et_lpassword.getText ( ).toString ( ) ;

  34. Boolean checklogin = databaseHelper.CheckLogin (username, password) ;

  35. if (checklogin == true ) {

  36. Toast.makeText (getApplicationContext( ), "Login Successful", Toast.LENGTH_SHORT ).show ( ) ;

  37. } else {

  38. Toast.makeText (getApplicationContext( ), "Invalid username or password", Toast.LENGTH_SHORT ).show ( ) ;

  39. }

  40. }

  41. } ) ;

  42. }

  43. }

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

  • 34743 views

How To Create Registration Page In Android Studio

Source: https://www.sourcecodester.com/android/12151/android-simple-registration-and-login-application.html

Posted by: carterthreatin1945.blogspot.com

0 Response to "How To Create Registration Page In Android Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel