Create login application where you will have to validate EmailID (UserName). Till the username and password is not validated , login button should remain disabled.
MainActivity.java
MainActivity.java
package com.example.assignment3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements OnClickListener, TextWatcher {
Button btnLogin, btnCancel;
EditText txtuname, txtpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin=(Button) findViewById(R.id.btnLogin);
btnCancel=(Button) findViewById(R.id.btnCancel);
txtuname=(EditText) findViewById(R.id.txtuname);
txtpass=(EditText)findViewById(R.id.txtpass);
btnLogin.setOnClickListener(this);
btnCancel.setOnClickListener(this);
btnLogin.setEnabled(false);
txtuname.addTextChangedListener(this);
txtpass.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int firstat_rat,lastat_rat,first_dot,last_dot;
boolean flag_email=true,flag_pass=true,flag=false;
String email,pass;
email=txtuname.getText().toString();
pass=txtpass.getText().toString();
firstat_rat=email.indexOf("@");
lastat_rat=email.lastIndexOf("@");
first_dot=email.indexOf(".");
last_dot=email.lastIndexOf(".");
if(firstat_rat<=0)
{
flag_email=false;
}
else if(firstat_rat!=lastat_rat )
{
flag_email=false;
}
else if(lastat_rat==email.length()-1)
{
flag_email=false;
}
else if(first_dot<= 0)
{
flag_email=false;
}
else if('.'==email.charAt(lastat_rat-1) ||'.'==email.charAt(lastat_rat+1))
{
flag_email=false;
}
else if(last_dot==email.length()-1)
{
flag_email=false;
}
else if(last_dot<lastat_rat)
{
flag_email=false;
}
else if(first_dot!=last_dot)
{
for(int i=first_dot; i<=last_dot; i++)
{
if(email.charAt(i)=='.')
{
if(flag==true)
{
flag_email=false;
break;
}
else
flag=true;
}
else
{
flag=false;
}
}
}
else
{
flag_email=true;
}
if(pass.equals(""))
{
flag_pass=false;
}
else
{
flag_pass=true;
}
if(flag_email==true && flag_pass==true )
btnLogin.setEnabled(true);
else
btnLogin.setEnabled(false);
}
@Override
public void onClick(View v) {
if(v.getId()==btnLogin.getId())
{
if(txtuname.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter User Name", Toast.LENGTH_SHORT).show();
}
else if(txtpass.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter Password", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this,"Login Successfully", Toast.LENGTH_SHORT).show();
}
}
else if(v.getId() == btnCancel.getId())
{
if(txtuname.getText().toString().equals("") && txtpass.getText().toString().equals(""))
{
Toast.makeText(this,"Text is Already Empty...",Toast.LENGTH_SHORT).show();
}
else
{
txtuname.setText("");
txtpass.setText("");
}
}
}
}
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/txtuname"
android:layout_width="252dp"
android:layout_height="44dp"
android:layout_marginBottom="52dp"
android:ems="10"
android:hint="Email Id"
android:inputType="textWebEmailAddress"
app:layout_constraintBottom_toTopOf="@+id/txtpass"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.449"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="@+id/txtpass"
android:layout_width="246dp"
android:layout_height="46dp"
android:layout_marginBottom="340dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/txtuname" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="39dp"
android:layout_marginRight="39dp"
android:layout_marginBottom="236dp"
android:text="login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnCancel"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="52dp"
android:layout_marginRight="52dp"
android:layout_marginBottom="236dp"
android:text="@android:string/cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.848"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output
To download follow this link : Program 3
Output
To download follow this link : Program 3
No comments:
Post a Comment