0% found this document useful (0 votes)
433 views

Practical No 28

The document describes a login form with validation implemented in Android. It includes an XML layout file with form fields like username, password, and a login button. It also has text views to display the number of login attempts remaining. The MainActivity class handles the login button click, validates the credentials, decrements the attempts remaining on failed login, and disables the login button if attempts are exhausted.

Uploaded by

darandalem505
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)
433 views

Practical No 28

The document describes a login form with validation implemented in Android. It includes an XML layout file with form fields like username, password, and a login button. It also has text views to display the number of login attempts remaining. The MainActivity class handles the login button click, validates the credentials, decrements the attempts remaining on failed login, and disables the login button if attempts are exhausted.

Uploaded by

darandalem505
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/ 3

PRACTICAL NO.

28
Login Form with Validation
XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authentication Required"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/passwordET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="testpass"
android:inputType="textPassword" />
<TextView
android:id="@+id/attemptsLeftTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:" />
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="authenticateLogin"
android:text="Login" />
<TextView
android:id="@+id/numberOfRemainingLoginAttemptsTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/loginLockedTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
</RelativeLayout>

MainActivity.java

package
com.examp1e.1oginva1i
date;import
android.widget.TextVi
ew; import
android.widget.Toast;
import
android.os.Bund1e;
public class MainActivity extends AppCompatActivity {
private
EditText
username;
private
EditText
password;
private
Button login;
private TextView
loginLockedTV; private
TextView
attemptsLeftTV;
private TextView
numberOfRemainingLoginAttemptsTV;int
numberOfRemainingLoginAttempts = 3;
@Override
protected void onCreate(Bund1e
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.1ayout.activity_ma
in);
username = (EditText)
findViewById(R.id.usernameET); password
= (EditText)
findViewById(R.id.passwordET); login =
(Button) findViewById(R.id.loginBtn);
loginLockedTV = (TextView)
findViewById(R.id.loginLockedTV); attemptsLeftTV
= (TextView) findViewById(R.id.attemptsLeftTV);
numberOfRemainingLoginAttemptsTV = (TextView)
findViewById(R.id.numberOfRemainingLoginAttemptsTV);
numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAt
tempts));
}
public void authenticateLogin(View view) {
if (username.getText().toString().equa1s("admin") &&
password.getText().toString().equa1s("admin")) {
Toast.makeText(getApp1icationContext(), "Login Successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApp1icationContext(), "Login
Unsuccessful!",
Toast.LENGTH_SHORT).show();
numberOfRemainingLoginAttempts--;
attemptsLeftTV.setVisibi1ity(View.VISIBLE);
numberOfRemainingLoginAttemptsTV.setVisibi1ity(View
.VISIBLE);
numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberO
fRemainingLoginAttempts)); if (numberOfRemainingLoginAttempts ==
0) {
login.setEnab1ed(false);
loginLockedTV.setVisibi1ity(Vie
w.VISIBLE);
loginLockedTV.setBackgroundCo1o
r(Co1or.RED);
loginLockedTV.setText("LOGIN
}
}
}
OUTPUT

You might also like