Basic Android Login Form Example

By | 2018-06-16T04:56:58+00:00 June 16th, 2018|android|1 Comment

In this tutorials, we are going to see how to create a simple Android Login Form using Android Studio.

Android Login Form:

Technologies:

  • Android SDK Version 27
  • Android AppCompact-v7:27.0.1
  • Gradle
  • Android Studio 3.1

Android Login Example:

Creating a simple Android login screen under Linear Layout.

Project Structure:

Android Login Form Example

Gradle Dependencies:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.onlinetutorialspoint.official.simplelogin"
        minSdkVersion 23
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.0.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

activity_main.xml

Creating a basic login form layout using LinearLayout having one text box, password and Login button.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.onlinetutorialspoint.official.simplelogin.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"

    android:layout_centerInParent="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="UserName"
        android:id="@+id/username"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="password"
    android:id="@+id/password"
    android:inputType="textPassword"
    />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:background="#3f76ff"
        android:textColor="#fff"
        android:id="@+id/login"/>
    
</LinearLayout>

</RelativeLayout>

MainActivity.java

Creating MainActivity.java, responsible to read the input from the about layout and validate the user inputs.

package com.onlinetutorialspoint.official.simplelogin;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {
EditText username,password;
Button login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        username=findViewById(R.id.username);
        password=findViewById(R.id.password);
        login=findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Objects.equals(username.getText().toString(), "admin")&&Objects.equals(password.getText().toString(),"admin"))
                {
                    Toast.makeText(MainActivity.this,"You have Authenticated Successfully",Toast.LENGTH_LONG).show();

                }else
                {
                    Toast.makeText(MainActivity.this,"Authentication Failed",Toast.LENGTH_LONG).show();
                }

            }
        });

    }
}

Run Application:

Run Android Virtual Device (AVD):

Andoid Login Form Example 2

Giving Valid Credentials: admin – admin

Andoid Login Form Example 3

Giving invalid credentials:

Andoid Login Form Example 4

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

One Comment

  1. nishareddy5879@gmail.com'
    nisha August 10, 2018 at 1:06 pm - Reply

    Its actually a great and helpful piece of information. I really want to learn android, I have entered in android with a great curiosity but today its become very difficult for me to learn, So please recommend me a startup….!
    Thank You

Leave A Comment