Android - Simple Login Application For Beginners

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

In this tutorial we will try to create a Simple Login Application Using Android. Android is basically a piece of software which 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 devices 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="<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.simpleloginapplication.Login">
  11.  
  12.  
  13. <TextView
  14. android:id="@+id/tv_login"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_alignParentLeft="true"
  18. android:layout_alignParentStart="true"
  19. android:layout_alignParentTop="true"
  20. android:layout_marginLeft="11dp"
  21. android:layout_marginStart="11dp"
  22. android:layout_marginTop="13dp"
  23. android:text="Login"
  24. android:fontFamily="sans-serif-condensed"
  25. android:textSize="30sp"/>
  26.  
  27.  
  28.  
  29. <TextView
  30. android:id="@+id/tv_username"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_alignLeft="@+id/tv_login"
  34. android:layout_alignStart="@+id/tv_login"
  35. android:layout_below="@+id/tv_login"
  36. android:layout_marginTop="80dp"
  37. android:fontFamily="monospace"
  38. android:text="Username"
  39. android:textSize="25sp" />
  40.  
  41. <EditText
  42. android:id="@+id/et_username"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_below="@+id/tv_username"
  46. android:ems="17"
  47. android:layout_alignLeft="@+id/tv_username" />
  48.  
  49.  
  50.  
  51. <TextView
  52. android:id="@+id/tv_password"
  53. android:layout_width="wrap_content"
  54. android:layout_height="wrap_content"
  55. android:layout_alignLeft="@+id/et_username"
  56. android:layout_alignStart="@+id/et_username"
  57. android:layout_below="@+id/et_username"
  58. android:layout_marginTop="33dp"
  59. android:fontFamily="monospace"
  60. android:text="Password"
  61. android:textSize="25sp" />
  62.  
  63. <EditText
  64. android:id="@+id/et_password"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:inputType="textPassword"
  68. android:ems="17"
  69. android:layout_below="@+id/tv_password"
  70. android:layout_alignLeft="@+id/tv_password" />
  71.  
  72.  
  73. android:id="@+id/btn_login"
  74. android:layout_height="wrap_content"
  75. android:layout_width="wrap_content"
  76. android:layout_below="@id/et_password"
  77. android:layout_centerInParent="true"
  78. android:ems="12"
  79. android:layout_marginTop="30dp"
  80. android:text="Login"/>
  81.  
  82.  
  83. </RelativeLayout>

Next is create a 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="<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.simpleloginapplication.User">
  11.  
  12. <TextView
  13. android:id="@+id/textView1"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Welcome"
  17. android:textSize="40sp"
  18. android:layout_marginTop="177dp"
  19. android:layout_alignParentTop="true"
  20. android:layout_centerHorizontal="true" />
  21.  
  22. <TextView
  23. android:id="@+id/textView2"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:textSize="30sp"
  27. android:layout_marginTop="30dp"
  28. android:layout_centerInParent="true"
  29. android:layout_below="@+id/textView1"
  30. android:text="Administrator"/>
  31.  
  32. </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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simpleloginapplication">
  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 android:name=".Login"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. <activity android:name=".User"
  23. android:configChanges="orientation"
  24. android:screenOrientation="portrait">
  25. <intent-filter>
  26. <action android:name="com.razormist.simpleloginapplication.User" />
  27.  
  28. <category android:name="android.intent.category.DEFAULT" />
  29. </intent-filter>
  30. </activity>
  31. </application>
  32. </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("com.razormist.simpleloginapplication.User");
  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 give you some ideas about android programming. 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.