Practical 28
public class MainActivity extends
Avtivity_main.xml AppCompatActivity {
<?xml version="1.0" encoding="utf-8"?>
EditText editTextUsername, editTextPassword;
<LinearLayout
Button btnLogin;
xmlns:android="http://schemas.android.com/apk/re
int loginAttempts = 0; // Track unsuccessful
s/android"
login attempts
xmlns:tools="http://schemas.android.com/tools"
final int MAX_ATTEMPTS = 3; // Maximum
android:layout_width="match_parent"
login attempts
android:layout_height="match_parent"
android:gravity="center"
@SuppressLint("MissingInflatedId")
android:orientation="vertical"
@Override
android:padding="20dp">
protected void onCreate(Bundle
savedInstanceState) {
<EditText
super.onCreate(savedInstanceState);
android:id="@+id/editTextUsername"
setContentView(R.layout.activity_main);
android:layout_width="match_parent"
android:layout_height="wrap_content"
editTextUsername =
android:hint="Enter Username"
findViewById(R.id.editTextUsername);
android:maxLines="1"
editTextPassword =
tools:ignore="TouchTargetSizeCheck" />
findViewById(R.id.editTextPassword);
btnLogin = findViewById(R.id.btnLogin);
<EditText
android:id="@+id/editTextPassword"
btnLogin.setOnClickListener(new
android:layout_width="match_parent"
View.OnClickListener() {
android:layout_height="wrap_content"
@Override
android:hint="Enter Password"
public void onClick(View v) {
android:inputType="textPassword"
String username =
android:maxLines="1"
editTextUsername.getText().toString().trim();
tools:ignore="TouchTargetSizeCheck" />
String password =
editTextPassword.getText().toString().trim();
<Button
android:id="@+id/btnLogin"
// Check if fields are empty
android:layout_width="match_parent"
if (username.isEmpty() ||
android:layout_height="wrap_content"
password.isEmpty()) {
android:text="Login" />
Toast.makeText(MainActivity.this,
"Username or Password cannot be empty",
</LinearLayout>
Toast.LENGTH_SHORT).show();
return;
}
MainActivity.java
package com.example.loginvalidation; // Check username length
if (username.length() < 4) {
import android.annotation.SuppressLint; Toast.makeText(MainActivity.this,
import android.os.Bundle; "Username must be at least 4 characters",
import android.view.View; Toast.LENGTH_SHORT).show();
import android.widget.Button; return;
import android.widget.EditText; }
import android.widget.Toast;
import // Check password length
androidx.appcompat.app.AppCompatActivity; if (password.length() < 6) {
Toast.makeText(MainActivity.this,
"Password must be at least 6 characters",
Toast.LENGTH_SHORT).show();
return;
}
// Dummy correct credentials
if (username.equals("admin") &&
password.equals("123456")) {
Toast.makeText(MainActivity.this,
"Login Successful!",
Toast.LENGTH_SHORT).show();
loginAttempts = 0; // Reset login
attempts
} else {
loginAttempts++;
if (loginAttempts >=
MAX_ATTEMPTS) {
Toast.makeText(MainActivity.this,
"Too many failed attempts! Try again later.",
Toast.LENGTH_LONG).show();
btnLogin.setEnabled(false); //
Disable login button after max attempts
} else {
Toast.makeText(MainActivity.this,
"Login Failed! Attempt " + loginAttempts,
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}