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

Mainactivity - Java: Savedinstancestate Savedinstancestate

This Java code defines a login activity with username and password fields and a login button. It checks for empty/short fields and validates the credentials, displaying toasts for errors or success. It tracks failed attempts and disables the button after too many tries.

Uploaded by

Bhakti Shingadi
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

Mainactivity - Java: Savedinstancestate Savedinstancestate

This Java code defines a login activity with username and password fields and a login button. It checks for empty/short fields and validates the credentials, displaying toasts for errors or success. It tracks failed attempts and disables the button after too many tries.

Uploaded by

Bhakti Shingadi
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

MainActivity.

java
package com.example.pr23;

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 EditText usernameEditText, passwordEditText;


private Button loginButton;

private int loginAttempts = 0;


private final int MAX_LOGIN_ATTEMPTS = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}
private void login() {
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
showToast("Username and password cannot be empty.");
return;
}
if (username.length() < 6 || password.length() < 6) {
showToast("Username and password must be at least 6 characters long.");
return;
}
// Simulate authentication - Replace this with actual authentication logic
if (username.equals("admin123") && password.equals("admin123")) {
showToast("Login Successful!");
// Proceed to next activity or action upon successful login
} else {
loginAttempts++;
if (loginAttempts >= MAX_LOGIN_ATTEMPTS) {
showToast("Maximum login attempts reached. Try again later.");
loginButton.setEnabled(false); // Disable login button after max attempts
} else {
showToast("Invalid username or password. Please try again. Attempts left: " +
(MAX_LOGIN_ATTEMPTS - loginAttempts));
}
}
}
private void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text"
tools:ignore="TouchTargetSizeCheck" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/usernameEditText"
android:layout_marginTop="16dp"
android:hint="Password"
android:inputType="textPassword"
tools:ignore="TouchTargetSizeCheck" />

<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/passwordEditText"
android:layout_marginTop="16dp"
android:text="Login"/>

</RelativeLayout>
Output

You might also like