Create and Login application as above. On successful login, open browser with any URL.
MainActivity.java
MainActivity.java
package com.example.assignment4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
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);
txtuname.addTextChangedListener(this);
txtpass.addTextChangedListener(this);
btnLogin.setEnabled(false);
}
@Override
public void onClick(View v) {
if(v.getId()==btnLogin.getId())
{
if(txtuname.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter UserName", Toast.LENGTH_SHORT).show();
}
else if(txtpass.getText().toString().equals(""))
{
Toast.makeText(this,"Please Enter Password", Toast.LENGTH_SHORT).show();
}
else
{
Intent myintent= new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
this.startActivity(myintent);
}
}
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("");
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
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 )
if(txtuname.getText().toString().equals("hgc@gmail.com") && txtpass.getText().toString().equals("123"))
{
btnLogin.setEnabled(true);
}
else
btnLogin.setEnabled(false);
else
btnLogin.setEnabled(false);
}
@Override
public void afterTextChanged(Editable s) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
tools:context=".MainActivity">
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="87dp"
android:layout_y="334dp"
android:text="Login"
tools:layout_editor_absoluteX="79dp"
tools:layout_editor_absoluteY="477dp" />
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="234dp"
android:layout_y="333dp"
android:text="Cancel"
tools:layout_editor_absoluteX="239dp"
tools:layout_editor_absoluteY="479dp" />
<EditText
android:id="@+id/txtuname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="101dp"
android:layout_y="129dp"
android:ems="10"
android:hint="Email Id"
android:inputType="textWebEmailAddress"
android:textColor="#000000"
android:textColorHint="#9C9494"
tools:layout_editor_absoluteX="100dp"
tools:layout_editor_absoluteY="195dp" />
<EditText
android:id="@+id/txtpass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="105dp"
android:layout_y="220dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#090909"
android:textColorHint="#9C9494"
tools:layout_editor_absoluteX="100dp"
tools:layout_editor_absoluteY="283dp" />
</AbsoluteLayout>
Output
The username is hgc@gmail.com and the password is 123
To download follow this link : Program 4
Output
The username is hgc@gmail.com and the password is 123
To download follow this link : Program 4
No comments:
Post a Comment