You are on page 1of 4

XML :-

<?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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
android:maxLength="10" />

<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="10" />
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Login" />
</LinearLayout>

Java :-
package com.example.a2728;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int MAX_LOGIN_ATTEMPTS = 3;
private int loginAttempts = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get the EditText fields and the login button


@SuppressLint({"MissingInflatedId", "LocalSuppress"}) final EditText et_username =
findViewById(R.id.et_username);
@SuppressLint({"MissingInflatedId", "LocalSuppress"}) final EditText et_password =
findViewById(R.id.et_password);
@SuppressLint({"MissingInflatedId", "LocalSuppress"}) final Button btn_login =
findViewById(R.id.btn_login);
// Set the click listener for the login button
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the username and password from the EditText fields
String username = et_username.getText().toString();
String password = et_password.getText().toString();
// Validate the username and password
if (username.isEmpty() || password.isEmpty()) {
// Display an error message for empty fields
Toast.makeText(MainActivity.this, "Please enter both username and
password", Toast.LENGTH_SHORT).show();
} else if (username.length() < 4 || password.length() < 4) {
// Display an error message for short username or password
Toast.makeText(MainActivity.this, "Username and password must be at least 4
characters long", Toast.LENGTH_SHORT).show();
} else {
// Check if the login is successful
if (username.equals("admin") && password.equals("password")) {
// Display the success message
Toast.makeText(MainActivity.this, "Login successful",
Toast.LENGTH_SHORT).show();
// Reset the login attempts
loginAttempts = 0;
} else {
// Increase the login attempts
loginAttempts++;
// Display an error message for failed login
if (loginAttempts < MAX_LOGIN_ATTEMPTS) {
Toast.makeText(MainActivity.this, "Invalid username or password. Please
try again", Toast.LENGTH_SHORT).show();
} else {
// Disable the login button after the maximum number of login attempts
btn_login.setEnabled(false);
Toast.makeText(MainActivity.this, "Maximum number of login attempts
reached. Please try again later.", Toast.LENGTH_SHORT).show();
}
}
}
}
});
}
}

You might also like