Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Exp28: 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:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<EditText
android:id="@+id/User_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password:"
android:inputType="textPassword"
android:layout_below="@+id/User_email"
android:layout_marginTop="50dp"
android:padding="10dp"/>

<TextView
android:id="@+id/User_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:textSize="40dp"
android:text="Login Form"/>

<Button
android:id="@+id/User_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/User_password"
android:layout_marginTop="50dp"
android:text="Login"/>
<EditText
android:id="@+id/User_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/User_Text"
android:layout_marginTop="50dp"
android:hint="Email:"
android:padding="10dp"
android:inputType="textEmailAddress"/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="~Developed By Harsh.Maghnani (Roll no:06)"
android:textColor="#0000FF"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="35dp"
android:layout_marginEnd="35dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.exp28;
import android.content.Intent;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button login;
EditText username,pass;
public String user,psd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

login = findViewById(R.id.User_login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
check();
}
});
username = findViewById(R.id.User_email);
pass = findViewById(R.id.User_password);
}
public void check(){
user = username.getText().toString().trim();
psd = pass.getText().toString().trim();

if(user.isEmpty()) {
username.setError("Enter an e-mail");
username.requestFocus();
return;
}
if(!Patterns.EMAIL_ADDRESS.matcher(user).matches()){
username.setError("Enter an e-mail");
username.requestFocus();
return;
}
if(psd.isEmpty()) {
pass.setError("Enter a password ");
pass.requestFocus();
return;
}
if(psd.length()<6){
pass.setError("Minimum length is 6 characters");
pass.requestFocus();
return;
}
if(user.equals("harsh.maghnani@gmail.com") &&
psd.equals("harsh123")) {
Toast.makeText(MainActivity.this,"Welcome User",
Toast.LENGTH_LONG).show();
Intent in = new Intent(MainActivity.this,NewActivity.class);
startActivity(in);
}
else{
Toast.makeText(this,"Wrong email or
password",Toast.LENGTH_LONG).show();
}}}
NewActivity.java:
package com.example.exp28;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class NewActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(NewActivity.this,"Welcome to
College",Toast.LENGTH_LONG).show();
}
}
Output:-

You might also like