Practical 28
Practical 28
Q1) WAP to create the login form with necessary validation like length of username and
password, empty text fields, count of unsuccessful login attempts. Display the login
successful / unsuccessful toast message.
XML CODE:
JAVA CODE:
package com.example.prac28;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uET = findViewById(R.id.E1);
pET = findViewById(R.id.E2);
b1 = findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = uET.getText().toString();
String password = pET.getText().toString();
if(username.isEmpty()||password.isEmpty()){
Toast.makeText(MainActivity.this,"Fields can't be Empty !!",Toast.LENGTH_SHORT).show();
} else if (username.equals("Nisha")&&password.equals("nisha234")){
Toast.makeText(MainActivity.this,"Login Successful !!",Toast.LENGTH_SHORT).show();
}else{
loginAttempts++;
if(loginAttempts >= 3){
b1.setEnabled(false);
Toast.makeText(MainActivity.this,"Too many Login Attempts",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"Incorrect Username or Password",Toast.LENGTH_SHORT).show();
}
}
}
});
}
}