banner



How To Create A Login Android App On Android Studio

Creating Simple Android Login Application

Submitted by razormist on Tuesday, January 12, 2021 - 10:06.

In this tutorial we will try to create a Simple Login Application Using Android. Android is basically a piece of software that allows your hardware to function. The android is an open-source operating system it's free and user friendly to mobile developers. Android is available to any device such as TV, phones, watches, etc.

So now let's 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 folder and select the activity_login.xml. Then copy and paste the code below.

activity_login.xml

              
  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.simpleloginapplication.Login" >

  8. <TextView

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

  10. android:layout_width= "wrap_content"

  11. android:layout_height= "wrap_content"

  12. android:layout_alignParentLeft= "true"

  13. android:layout_alignParentStart= "true"

  14. android:layout_alignParentTop= "true"

  15. android:layout_marginLeft= "11dp"

  16. android:layout_marginStart= "11dp"

  17. android:layout_marginTop= "13dp"

  18. android:text= "Login"

  19. android:fontFamily= "sans-serif-condensed"

  20. android:textSize= "30sp" />

  21. <TextView

  22. android:id= "@+id/tv_username"

  23. android:layout_width= "wrap_content"

  24. android:layout_height= "wrap_content"

  25. android:layout_alignLeft= "@+id/tv_login"

  26. android:layout_alignStart= "@+id/tv_login"

  27. android:layout_below= "@+id/tv_login"

  28. android:layout_marginTop= "80dp"

  29. android:fontFamily= "monospace"

  30. android:text= "Username"

  31. android:textSize= "25sp" />

  32. <EditText

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

  34. android:layout_width= "wrap_content"

  35. android:layout_height= "wrap_content"

  36. android:layout_below= "@+id/tv_username"

  37. android:ems= "17"

  38. android:layout_alignLeft= "@+id/tv_username" />

  39. <TextView

  40. android:id= "@+id/tv_password"

  41. android:layout_width= "wrap_content"

  42. android:layout_height= "wrap_content"

  43. android:layout_alignLeft= "@+id/et_username"

  44. android:layout_alignStart= "@+id/et_username"

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

  46. android:layout_marginTop= "33dp"

  47. android:fontFamily= "monospace"

  48. android:text= "Password"

  49. android:textSize= "25sp" />

  50. <EditText

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

  52. android:layout_width= "wrap_content"

  53. android:layout_height= "wrap_content"

  54. android:inputType= "textPassword"

  55. android:ems= "17"

  56. android:layout_below= "@+id/tv_password"

  57. android:layout_alignLeft= "@+id/tv_password" />

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

  59. android:layout_height= "wrap_content"

  60. android:layout_width= "wrap_content"

  61. android:layout_below= "@id/et_password"

  62. android:layout_centerInParent= "true"

  63. android:ems= "12"

  64. android:layout_marginTop= "30dp"

  65. android:text= "Login" />

  66. </RelativeLayout>

Next is create another layout by right-clicking the layout folder namely activity_user.xml. Then write these blocks code to the layout script.

activity_user.xml

              
  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.simpleloginapplication.User" >

  8. <TextView

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

  10. android:layout_width= "wrap_content"

  11. android:layout_height= "wrap_content"

  12. android:text= "Welcome"

  13. android:textSize= "40sp"

  14. android:layout_marginTop= "177dp"

  15. android:layout_alignParentTop= "true"

  16. android:layout_centerHorizontal= "true" />

  17. <TextView

  18. android:id= "@+id/textView2"

  19. android:layout_width= "wrap_content"

  20. android:layout_height= "wrap_content"

  21. android:textSize= "30sp"

  22. android:layout_marginTop= "30dp"

  23. android:layout_centerInParent= "true"

  24. android:layout_below= "@+id/textView1"

  25. android:text= "Administrator" />

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

              
  1. <?xml version= "1.0" encoding= "utf-8" ?>

  2. <manifest xmlns:android= "http://schemas.android.com/apk/res/android"

  3. package = "com.razormist.simpleloginapplication" >

  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 android:name= ".Login"

  12. android:configChanges= "orientation"

  13. android:screenOrientation= "portrait" >

  14. <intent-filter>

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

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

  17. </intent-filter>

  18. </activity>

  19. <activity android:name= ".User"

  20. android:configChanges= "orientation"

  21. android:screenOrientation= "portrait" >

  22. <intent-filter>

  23. <action android:name= "com.razormist.simpleloginapplication.User" />

  24. <category android:name= "android.intent.category.DEFAULT" />

  25. </intent-filter>

  26. </activity>

  27. </application>

  28. </manifest>

The Main Function

Login.java

This code contains the main function of the application. This code will login the user when the username and password are entered correctly. To create the function just write the code inside the Login class

              
  1. package com.razormist.simpleloginapplication ;

  2. import android.content.Intent ;

  3. import android.support.v7.app.AppCompatActivity ;

  4. import android.os.Bundle ;

  5. import android.util.Log ;

  6. import android.view.View ;

  7. import android.widget.Button ;

  8. import android.widget.EditText ;

  9. import android.widget.Toast ;

  10. public class Login extends AppCompatActivity {

  11. EditText et_username, et_password;

  12. @Override

  13. protected void onCreate(Bundle savedInstanceState) {

  14. super.onCreate (savedInstanceState) ;

  15. setContentView(R.layout.activity_login ) ;

  16. Login( ) ;

  17. }

  18. void Login( ) {

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

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

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

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

  23. @Override

  24. public void onClick( View v) {

  25. if (et_username.getText ( ).toString ( ).equals ( "admin" ) && et_password.getText ( ).toString ( ).equals ( "admin" ) ) {

  26. Toast.makeText (Login.this, "Username and Password is correct", Toast.LENGTH_SHORT ).show ( ) ;

  27. Intent intent = new Intent(Login.this,User.Class ) ;

  28. startActivity(intent) ;

  29. } else {

  30. Toast.makeText (Login.this, "Username or Password is incorrect", Toast.LENGTH_SHORT ).show ( ) ;

  31. }

  32. }

  33. } ) ;

  34. }

  35. }

User.java

This code will render a new layout after the user successfully login. This is where the user is redirect after entering the correction information. Just write these block of codes inside the User class.

              
  1. package com.razormist.simpleloginapplication ;

  2. import android.support.v7.app.AppCompatActivity ;

  3. import android.os.Bundle ;

  4. public class User extends AppCompatActivity {

  5. @Override

  6. protected void onCreate(Bundle savedInstanceState) {

  7. super.onCreate (savedInstanceState) ;

  8. setContentView(R.layout.activity_user ) ;

  9. }

  10. }

Try to run and see if it works:

username: admin
password: admin

There you have it we successfully created a Simple Login Application using Android. I hope that this tutorial gives you some ideas about android programming. For more updates and tutorials just kindly visit this site.

Enjoy Coding.

  • 22209 views

How To Create A Login Android App On Android Studio

Source: https://www.sourcecodester.com/android/11968/android-simple-login-application-beginners.html

Posted by: johnstontiledgets.blogspot.com

0 Response to "How To Create A Login Android App On Android Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel