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

Android Login Form with Validation

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)
91 views4 pages

Android Login Form with Validation

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

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="[Link]
android:background="#ffffff"
es/android"
android:hint="Enter Username" />
xmlns:tools="[Link]
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" />
[Link](savedInstanceState);
</LinearLayout>
setContentView([Link].activity_main);
<TextView
android:id="@+id/textView1"
setContentView([Link].activity_main);
android:layout_width="wrap_content"
loginbtn =
android:layout_height="wrap_content"
(Button)findViewById([Link]);
android:textColor="#ffffff"
clrbtn = (Button)findViewById([Link]);
android:textSize="20dp"
uname =
android:text=" Username = Servive0 \n (EditText)findViewById([Link]);
Password = Servive@0"/>
pwd = (EditText)findViewById([Link]);

</LinearLayout>
[Link](new
</LinearLayout> [Link]() {
public void onClick(View v) {

[Link] getuname = [Link]().toString();

package [Link].ex2801; getpwd = [Link]().toString();


if([Link]("") &&
[Link]("")) {
import
[Link];
[Link]([Link],"Username and
import [Link]; Password should not be
import [Link]; empty",Toast.LENGTH_SHORT).show();

import [Link]; } else {

import [Link]; if([Link]() < 8) {

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

String getuname, getpwd;


must be of at-least 8 characters",Toast.LENGTH_SHORT).show();
} else {
if([Link]("Servive0") && [Link]("Servive@0")){
[Link]([Link],
"Login successful", Toast.LENGTH_LONG).show();
} else {
count++;
[Link]([Link],
"Login failed\n Attempt "+count, Toast.LENGTH_LONG).show();

}
}

}
}

[Link](new [Link]() {
public void onClick(View v) {
[Link]("");
[Link]("");
}
});
}
});
}
}
Output:

You might also like