Create login application where you will have to validate EmailID(UserName). Till the user name and password is not validated , login button should remain disabled.
File Name : E3Activity.java
package bsr.exa;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class E3Activity extends Activity {
/** @author Bipin S Rupadiya , www.BipinRupadiya.blogspot.in
*
* 3) Create login application where you will have to validate EmailID(UserName).
* Till the user name and password is not validated , login button should remain disabled.
**/
String uname="rupadiyabipin@gmail.com";
String pwd="bipin";
boolean usrStatus=false;
boolean pwdStatus=false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create an intent
final Intent intent=new Intent(this,Screen2.class);
final Button cmdLogin = (Button) findViewById(R.id.cmdLogin);
final EditText txtUsr = (EditText)findViewById(R.id.txtUsr);
final EditText txtPwd = (EditText)findViewById(R.id.txtPwd);
cmdLogin.setEnabled(false);
cmdLogin.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
intent.putExtra("uname1", txtUsr.getText().toString());
intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//Toast.makeText(E3Activity.this, "welcome ".txtUser.getText().toString().concat(" you are authenticated user! "), Toast.LENGTH_LONG).show();
}
});
//------------------------------------------------
//clear event
final Button btnClear = (Button)findViewById(R.id.cmdClear);
btnClear.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
EditText fldu=(EditText)findViewById(R.id.txtUsr);
EditText fldp=(EditText)findViewById(R.id.txtPwd);
fldu.setText("");
fldp.setText("");
}
});
//------------------------------------------------
txtUsr.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
if(uname.equals(txtUsr.getText().toString()))
{
usrStatus=true;
}
else
{
usrStatus=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(usrStatus && pwdStatus)
{
cmdLogin.setEnabled(true);
}
else
{
cmdLogin.setEnabled(false);
}
}
});
txtPwd.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
if(pwd.equals(txtPwd.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(usrStatus && pwdStatus)
{
cmdLogin.setEnabled(true);
}
else
{
cmdLogin.setEnabled(false);
}
}
});
}
}
File Name : Screen2.java
package bsr.exa;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/** @author Bipin S Rupadiya , www.gtu-android.blogspot.com
*
*
*
**/
public class Screen2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent=getIntent();
String uname=intent.getStringExtra("uname1");
TextView tv=new TextView(this);
Button btnBack = new Button(this);
btnBack.setText("<<Back");
tv.setText("Welcome "+uname+" !");
tv.setTextColor(Color.rgb(255, 255, 255));
tv.setTextSize(25);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(tv);
ll.addView(btnBack);
setContentView(ll);
//------------------------------------------------
//back event
btnBack.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
setContentView(R.layout.main);
}
});
//------------------------------------------------
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bsr.exa"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".E3Activity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Screen2"></activity>
</application>
</manifest>

Good work
ReplyDelete