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:
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):

Giving Valid Credentials: admin – admin

Giving invalid credentials:

Happy Learning 🙂
