In this tutorial, we will create the login form with necessary validations like length of username and password, empty text fields, count of unsuccessful login attempts. And will display the login successful/Unsuccessful toast message
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loginvalidateapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login Form"
android:layout_marginTop="100dp"
android:textSize="35sp"
android:textColor="@color/colorAccent"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:layout_marginTop="100dp"
android:textSize="20sp"/>
<EditText android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password"
android:textSize="20sp"/>
<EditText android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textSize="20sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:onClick="login" />
</LinearLayout>
package com.example.loginvalidateapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etUsername,etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
}
public void login(View view) {
String msg = validateUser();
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
private String validateUser() {
String uname = etUsername.getText().toString();
String pwd = etPassword.getText().toString();
if(uname.length() != 5 || pwd.length() != 5) {
return "Username and password must have 5 characters";
}
if(uname.equals("") || pwd.equals("")) {
return "Plz enter both username and password";
}
if(uname.equals("admin") && pwd.equals("12345")) {
return "Login Successful";
}
else {
return "Login Unuccessful";
}
}
}