0% found this document useful (0 votes)
14 views2 pages

Practical 28

The document contains code for an Android login application using Java. It includes a user interface with fields for username and password, and implements validation checks for input length and correct credentials. The application tracks login attempts and disables the login button after a maximum number of failed attempts.

Uploaded by

janhavirhude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Practical 28

The document contains code for an Android login application using Java. It includes a user interface with fields for username and password, and implements validation checks for input length and correct credentials. The application tracks login attempts and disables the login button after a maximum number of failed attempts.

Uploaded by

janhavirhude
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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();
}
}
}
});
}
}

You might also like