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

Practical 28

The document provides a Java program to create a login form with validations for username and password length, empty fields, and tracking unsuccessful login attempts. It includes code snippets for both the Java activity and the corresponding XML layout. The program displays messages for successful or failed login attempts and disables the login button after three unsuccessful tries.

Uploaded by

omkokate555
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)
9 views2 pages

Practical 28

The document provides a Java program to create a login form with validations for username and password length, empty fields, and tracking unsuccessful login attempts. It includes code snippets for both the Java activity and the corresponding XML layout. The program displays messages for successful or failed login attempts and disables the login button after three unsuccessful tries.

Uploaded by

omkokate555
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 no.

28
Write a program to create login form with necessary validations like length of ID & PW, empty field, count
of unsuccessful PW attempts. Display login successful / unsuccessful.
Java Code- package com.example.madpracticalno28;
import androidx.appcompat.app.AppCompatActiuity:
importandroid.os.Bundles
imp android.view.View:
import android.uidg et.Buttoni
import android.widget.Edi kti

import android.widqet.Toast;
public class MainActivity extends AppCompatActivity

{
private EditText etUsername, etPassuword:
private Button btnLogini
private int unsuccesstulAttempts =0:
private String correctUsername ="userl23":
private String correctPassword ="password123";
@Override
protected void onCreate(BundlesauedlnstanceState)
super.onCreate(sauedinstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewByld(R.id.etUsername);
etPassword =findViewByld(R.id.etPassword);
btnLogin =
findViewByld(R.ld.btnLogin)
btnLogin.setOn r(new Vleuw.OnClickListener() {
@Override
public void onClick(View u)

Stringusername etUsername.get Text(). toString().trim():


String password = etPassword.getText().toString().trim();
if (username.isEmpty) | password.isEmpty())
Toast.makeText(MainÄctivity.this, "Please enter both username and password", Toast.LENGTH_SHORT).show():
}else if (username.length() <6||password.length() <6) (
Toast.makeText(MainActiuity.this, "Username and password must be at least 6 characters long",
Toast.LENGTH_SHORT).show():
)else
if 88
(username.equals(correctUsername) password.equals(correctPassword) {

Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();


Jelse
unsuccessfulAttempts++;
if (unsuccessfulAttempts >= 3)
btnLogin.setEnabled(false)
Toast.makeText (MainActiuity.this, "Login failed. You haue reached maximum attempts.",
Toast.LENGTH_SHORT).show():
) else

Toast.makeText (MainActivity.this, "Login falled. Please try again. Attempt:" + unsuccessfulAttempts,


Toast.LENGTH_SHORT).show):))))

XML Fille- <RelatiueLayout xmlns:android="http://schemas.android.com/apk/res/android"


xmlns:tools="http://schemas.androld.com/tools"
android:layout_width="match_parent"
android:layout_height="match_ parent"
androidpaddingLeft="16dp"
android:padding "16dp"
ndroid:paddinaRioht="thdn"
android:paddingBottom="16dp"
tools:context=".MainActiuity">
<EditText

android:id="@+id/etUsername"
androld:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="uwrap_content"
android:laout below="@id/etUsername"
and
it_marginTop="8dp"
Password"

android:inputType="textPassword" />
<Button

android:id="@+id/btnLogin"
androld:layout_wldth="match_parent"
android:layout_height="uwrap_content"
android:layout_below="@id/etPassword"
android:layout_marginTop="16dp"
android:text="Login" />
</RelativeLayout>
Output

4439 443D 443Dh

D
MAD Practical no. 28 MAD Practical no, 28 MAD Practical no. 28

Username vjuggigiviv user123

Password

LOgIN LOGIN LOGIN

Please bothusemame and Username and passwordust beat


enter
Login tuccessful
pasword 6 characters
least
long
443 D

MAD Practical no. 28

User123

LOGIN

Logn You have reached


faled.
maimumattempts

You might also like