0% found this document useful (0 votes)
20 views4 pages

EX 28

The document outlines the creation of a login form in Android with necessary validations for username and password length, empty fields, and tracking unsuccessful login attempts. It includes XML layout code for the user interface and Java code for the functionality, including toast messages for login success or failure. The program also features a clear button to reset the input fields.

Uploaded by

Omkar Mankar
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)
20 views4 pages

EX 28

The document outlines the creation of a login form in Android with necessary validations for username and password length, empty fields, and tracking unsuccessful login attempts. It includes XML layout code for the user interface and Java code for the functionality, including toast messages for login success or failure. The program also features a clear button to reset the input fields.

Uploaded by

Omkar Mankar
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/ 4

X.

Exercise
1. Write a program to create the login form with necessary validations like length of username and
password, empty text fields, count of unsuccessful login attempts. Display the login
successful/Unsuccessful toast message.

activity_main.xml android:layout_width="250dp"
android:layout_height="40dp"
<?xml version="1.0" encoding="utf-8"?> android:layout_gravity="center"
<LinearLayout android:layout_marginTop="40dp"
xmlns:android="http://schemas.android.com/apk/r
android:background="#ffffff"
es/android"
android:hint="Enter Username" />
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
<EditText
android:layout_width="match_parent"
android:id="@+id/pwd"
android:layout_height="match_parent"
android:layout_width="250dp"
android:orientation="vertical"
android:layout_height="40dp"
tools:context=".MainActivity" >
android:layout_gravity="center"
<TextView
android:layout_marginTop="10dp"
android:id="@+id/textView"
android:layout_marginBottom="20dp"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:text="Login"
android:inputType="textPassword" />
android:layout_marginTop="80dp"
<LinearLayout
android:layout_gravity="center"
android:layout_width="match_parent"
android:textAppearance="?android:attr/textAppea android:layout_height="wrap_content"
ranceLarge" />
android:orientation="horizontal"
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="wrap_content"
android:gravity="center" >
android:orientation="vertical"
<Button
android:layout_marginTop="50dp"
android:id="@+id/loginbtn"
android:background="#9DA7D0">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<EditText
android:text="Login" />
android:id="@+id/uname"
<Button int count = 0;
android:id="@+id/clrbtn"
android:layout_width="wrap_content" @Override
android:layout_height="wrap_content" protected void onCreate(Bundle
savedInstanceState) {
android:text="Clear" />
super.onCreate(savedInstanceState);
</LinearLayout>
setContentView(R.layout.activity_main);
<TextView
android:id="@+id/textView1"
setContentView(R.layout.activity_main);
android:layout_width="wrap_content"
loginbtn =
android:layout_height="wrap_content"
(Button)findViewById(R.id.loginbtn);
android:textColor="#ffffff"
clrbtn = (Button)findViewById(R.id.clrbtn);
android:textSize="20dp"
uname =
android:text=" Username = Servive0 \n (EditText)findViewById(R.id.uname);
Password = Servive@0"/>
pwd = (EditText)findViewById(R.id.pwd);

</LinearLayout>
loginbtn.setOnClickListener(new
</LinearLayout> View.OnClickListener() {
public void onClick(View v) {

MainActivity.java getuname = uname.getText().toString();

package com.example.ex2801; getpwd = pwd.getText().toString();


if(getuname.equals("") &&
getpwd.equals("")) {
import
androidx.appcompat.app.AppCompatActivity;
Toast.makeText(MainActivity.this,"Username and
import android.os.Bundle; Password should not be
import android.view.View; empty",Toast.LENGTH_SHORT).show();

import android.widget.Button; } else {

import android.widget.EditText; if(getuname.length() < 8) {

import android.widget.Toast;
Toast.makeText(MainActivity.this,"Username
must be of at-least 8
public class MainActivity extends characters",Toast.LENGTH_SHORT).show();
AppCompatActivity { } else {
if (getpwd.length() < 8) {
Button loginbtn, clrbtn;
EditText uname, pwd; Toast.makeText(MainActivity.this,"Password

String getuname, getpwd;


must be of at-least 8 characters",Toast.LENGTH_SHORT).show();
} else {
if(getuname.matches("Servive0") && getpwd.matches("Servive@0")){
Toast.makeText(MainActivity.this,
"Login successful", Toast.LENGTH_LONG).show();
} else {
count++;
Toast.makeText(MainActivity.this,
"Login failed\n Attempt "+count, Toast.LENGTH_LONG).show();

}
}

}
}

clrbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
uname.setText("");
pwd.setText("");
}
});
}
});
}
}
Output:

You might also like