*********Java file**********
import android.app.Activity;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.text.method.*;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnClickListener;
public class ThirdActivity extends Activity {
/** Called when the activity is first created. */
String uname="ronak";
String pwd="password";
boolean idstatus=false;
boolean pwdstatus=false;
Toast myToast;
static int toast_count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final Button lbutton=(Button)findViewById(R.id.button1);
final EditText etUsername=(EditText)findViewById(R.id.editText1);
final EditText etPwd=(EditText)findViewById(R.id.editText2);
etUsername.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
String data=arg0.toString();
if(isEmail(data))
{
/* if(myToast!=null)
{
myToast.cancel();
}*/
if(toast_count==0)
{
myToast=Toast.makeText(ThirdActivity.this, "Email or Username is correct", Toast.LENGTH_LONG);
myToast.show();
}
toast_count=1;
if(uname.equals(etUsername.getText().toString()))
{
idstatus=true;
}
else
{
idstatus=false;
}
}
else
{
idstatus=false;
toast_count=0;
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
if(idstatus && pwdstatus)
{
lbutton.setText("Login me");
lbutton.setEnabled(true);
}
else
{
lbutton.setText(null);
lbutton.setEnabled(false);
}
}
});
etPwd.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
if(pwd.equals(etPwd.getText().toString()))
{
pwdstatus=true;
}
else
{
pwdstatus=false;
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
if(idstatus && pwdstatus)
{
lbutton.setText("Login me");
lbutton.setEnabled(true);
}
else
{
lbutton.setText(null);
lbutton.setEnabled(false);
}
}
});
}
protected boolean isEmail(String etUsername)
{
int i=1;
int len=etUsername.length();
while((i<len) && (etUsername.charAt(i)!='@'))
{
i++;
}
if((i>=len) || (etUsername.charAt(i)!='@'))
{
return false;
}
else
{
i+=2;
}
while((i<len) && (etUsername.charAt(i)!='.'))
{
i++;
}
if((i>=len-1) || (etUsername.charAt(i)!='.'))
{
return false;
}
else
{
return true;
}
}
}
************XML File**************
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:orientation="vertical"
android:gravity="center" android:background="@drawable/desktop_3">
<LinearLayout android:layout_height="272px" android:layout_width="305px" android:orientation="vertical" android:gravity="center" android:background="@drawable/desktop_7">
<TableLayout
android:layout_width="wrap_content" android:background="@color/Bgcolor"
android:layout_height="wrap_content" android:gravity="center" android:padding="10px">
<TableRow>
<TextView android:text="@string/LoginTitle" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="@color/Fgcolor" android:padding="10px" android:textSize="25px">
</TextView>
</TableRow>
</TableLayout>
<ScrollView android:layout_height="wrap_content" android:layout_width="wrap_content">
<TableLayout
android:layout_width="wrap_content" android:background="@color/Bgcolor"
android:layout_height="wrap_content" android:gravity="center" android:padding="10px">
<TableRow
android:gravity="center"
android:orientation="vertical" >
<TextView android:text="Username: " android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@color/Fgcolor">
</TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_weight="1" android:layout_width="wrap_content"
android:gravity="center_vertical" android:hint="Username">
<requestFocus></requestFocus>
</EditText>
</TableRow>
<TableRow
android:gravity="center"
android:orientation="vertical">
<TextView android:text="Password:" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@color/Fgcolor">
</TextView>
<EditText android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_weight="1" android:layout_width="wrap_content" android:inputType="textPassword"
android:gravity="center_vertical" android:hint="Password"></EditText>
</TableRow>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:typeface="sans" android:hint="Enables only when all fields are correct"
android:textColor="@color/Bgcolor1" android:textStyle="italic"
android:enabled="false" android:layout_marginTop="15px"></Button>
</TableLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
No comments:
Post a Comment