0% found this document useful (0 votes)
17 views3 pages

Practical 28

Uploaded by

Nisha Parchande
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)
17 views3 pages

Practical 28

Uploaded by

Nisha Parchande
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 – 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:

<?xml version="1.0" encoding="utf-8"?> app:layout_constraintBottom_toTopOf="@+id/E1"


<androidx.constraintlayout.widget.ConstraintLayout app:layout_constraintStart_toStartOf="@+id/E1"
xmlns:android="http://schemas.android.com/apk/res/an app:layout_constraintTop_toBottomOf="@+id/t1"
droid" />
xmlns:app="http://schemas.android.com/apk/res-
auto" <EditText
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/E1"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:padding="20dp" android:layout_marginBottom="44dp"
tools:context=".MainActivity"> android:ems="10"
android:hint="Username"
<ImageView android:inputType="text"
android:id="@+id/imageView" app:layout_constraintBottom_toTopOf="@+id/t3"
android:layout_width="217dp" app:layout_constraintEnd_toEndOf="parent"
android:layout_height="133dp" app:layout_constraintStart_toStartOf="parent"
android:layout_gravity="center_horizontal" app:layout_constraintTop_toBottomOf="@+id/t2"
android:layout_marginStart="80dp" />
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp" <TextView
app:layout_constraintBottom_toTopOf="@+id/t1" android:id="@+id/t3"
app:layout_constraintStart_toStartOf="parent" android:layout_width="wrap_content"
app:layout_constraintTop_toTopOf="parent" android:layout_height="wrap_content"
android:layout_marginStart="1dp"
app:srcCompat="@drawable/undraw_secure_login_pdn android:layout_marginBottom="8dp"
4" /> android:text="Enter Password"
app:layout_constraintBottom_toTopOf="@+id/E2"
<TextView app:layout_constraintStart_toStartOf="@+id/E2"
android:id="@+id/t1" app:layout_constraintTop_toBottomOf="@+id/E1"
android:layout_width="wrap_content" />
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" <EditText
android:layout_marginStart="68dp" android:id="@+id/E2"
android:layout_marginBottom="55dp" android:layout_width="wrap_content"
android:text="Login" android:layout_height="wrap_content"
android:textSize="30dp" android:layout_marginBottom="60dp"
android:textStyle="bold" android:ems="10"
app:layout_constraintBottom_toTopOf="@+id/t2" android:hint="Password"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="@+id/imageView
" app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageVie app:layout_constraintStart_toStartOf="parent"
w" /> app:layout_constraintTop_toBottomOf="@+id/t3"
/>
<TextView
android:id="@+id/t2" <Button
android:layout_width="wrap_content" android:id="@+id/button"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginStart="2dp" android:layout_height="wrap_content"
android:layout_marginBottom="9dp" android:layout_marginBottom="163dp"
android:text="Enter Username" android:text="LOGIN"
android:textStyle="bold" app:layout_constraintTop_toBottomOf="@+id/E2"
app:layout_constraintBottom_toBottomOf="parent" />
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" </androidx.constraintlayout.widget.ConstraintLayout>

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;

public class MainActivity extends AppCompatActivity {


private EditText uET, pET;
private Button b1;
private int loginAttempts = 0;

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

You might also like