Creating Simple Android Login Application

Operating System

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.  
  9.  
  10. <TextView
  11. android:id="@+id/tv_login"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentLeft="true"
  15. android:layout_alignParentStart="true"
  16. android:layout_alignParentTop="true"
  17. android:layout_marginLeft="11dp"
  18. android:layout_marginStart="11dp"
  19. android:layout_marginTop="13dp"
  20. android:text="Login"
  21. android:fontFamily="sans-serif-condensed"
  22. android:textSize="30sp"/>
  23.  
  24.  
  25.  
  26. <TextView
  27. android:id="@+id/tv_username"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_alignLeft="@+id/tv_login"
  31. android:layout_alignStart="@+id/tv_login"
  32. android:layout_below="@+id/tv_login"
  33. android:layout_marginTop="80dp"
  34. android:fontFamily="monospace"
  35. android:text="Username"
  36. android:textSize="25sp" />
  37.  
  38. <EditText
  39. android:id="@+id/et_username"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_below="@+id/tv_username"
  43. android:ems="17"
  44. android:layout_alignLeft="@+id/tv_username" />
  45.  
  46.  
  47.  
  48. <TextView
  49. android:id="@+id/tv_password"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_alignLeft="@+id/et_username"
  53. android:layout_alignStart="@+id/et_username"
  54. android:layout_below="@+id/et_username"
  55. android:layout_marginTop="33dp"
  56. android:fontFamily="monospace"
  57. android:text="Password"
  58. android:textSize="25sp" />
  59.  
  60. <EditText
  61. android:id="@+id/et_password"
  62. android:layout_width="wrap_content"
  63. android:layout_height="wrap_content"
  64. android:inputType="textPassword"
  65. android:ems="17"
  66. android:layout_below="@+id/tv_password"
  67. android:layout_alignLeft="@+id/tv_password" />
  68.  
  69.  
  70. android:id="@+id/btn_login"
  71. android:layout_height="wrap_content"
  72. android:layout_width="wrap_content"
  73. android:layout_below="@id/et_password"
  74. android:layout_centerInParent="true"
  75. android:ems="12"
  76. android:layout_marginTop="30dp"
  77. android:text="Login"/>
  78.  
  79.  
  80. </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.  
  9. <TextView
  10. android:id="@+id/textView1"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="Welcome"
  14. android:textSize="40sp"
  15. android:layout_marginTop="177dp"
  16. android:layout_alignParentTop="true"
  17. android:layout_centerHorizontal="true" />
  18.  
  19. <TextView
  20. android:id="@+id/textView2"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:textSize="30sp"
  24. android:layout_marginTop="30dp"
  25. android:layout_centerInParent="true"
  26. android:layout_below="@+id/textView1"
  27. android:text="Administrator"/>
  28.  
  29. </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.  
  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 android:name=".Login"
  13. android:configChanges="orientation"
  14. android:screenOrientation="portrait">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. <activity android:name=".User"
  22. android:configChanges="orientation"
  23. android:screenOrientation="portrait">
  24. <intent-filter>
  25. <action android:name="com.razormist.simpleloginapplication.User" />
  26.  
  27. <category android:name="android.intent.category.DEFAULT" />
  28. </intent-filter>
  29. </activity>
  30. </application>
  31. </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.  
  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 Login extends AppCompatActivity {
  13.  
  14. EditText et_username, et_password;
  15. Button btn_login;
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_login);
  21.  
  22. Login();
  23. }
  24.  
  25. void Login(){
  26. et_username = (EditText)findViewById(R.id.et_username);
  27. et_password = (EditText)findViewById(R.id.et_password);
  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. if(et_username.getText().toString().equals("admin") && et_password.getText().toString().equals("admin")){
  34. Toast.makeText(Login.this, "Username and Password is correct", Toast.LENGTH_SHORT).show();
  35. Intent intent = new Intent(Login.this,User.Class);
  36. startActivity(intent);
  37. }else{
  38. Toast.makeText(Login.this, "Username or Password is incorrect", Toast.LENGTH_SHORT).show();
  39. }
  40. }
  41. });
  42. }
  43. }
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.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5.  
  6. public class User extends AppCompatActivity {
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_user);
  12. }
  13. }

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.

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 bySahil Bhagat (not verified)on Wed, 06/21/2023 - 01:13

Brand store

Add new comment