Exercise
: 3
Create login
application where you will have to validate EmailID(UserName). Till the
username and password is not validated , login button should remain disabled.
Screen Shot:
XML :
main.xml :
JAVA :
ThirdActivity.java :
package kmn.Third;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ThirdActivity extends Activity implements OnClickListener,TextWatcher
{
/**@author GTU MCA PPT, www.gtumcappt.com */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText txtemail = (EditText)findViewById(R.id.txtemail);
EditText txtpassword = (EditText)findViewById(R.id.txtpassword);
txtemail.addTextChangedListener(this);
txtpassword.addTextChangedListener(this);
Button btn = (Button)findViewById(R.id.btnlogin);
btn.setOnClickListener(this);
}
public void onClick(View v)
{
EditText txte = (EditText)findViewById(R.id.txtemail);
EditText txtp = (EditText)findViewById(R.id.txtpassword);
if(txte.getText().toString().equals("keval") && txtp.getText().toString().equals("nagaria"))
{
Intent i = new Intent(this,S2.class);
i.putExtra("txte", txte.getText().toString());
startActivity(i);
//Toast.makeText(this, "Login...",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Invalid",Toast.LENGTH_LONG).show();
}
}
public void afterTextChanged(Editable s)
{
EditText txte = (EditText)findViewById(R.id.txtemail);
EditText txtp = (EditText)findViewById(R.id.txtpassword);
Button btn = (Button)findViewById(R.id.btnlogin);
if(txte.getText().toString().equals("keval") && txtp.getText().toString().equals("nagaria"))
{
btn.setEnabled(true);
}
else
{
btn.setEnabled(false);
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
}
S2.java :
package kmn.Third;
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;
public class S2 extends Activity
{
/**@author GTU MCA PPT, www.gtumcappt.com */
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i= getIntent();
//Toast.makeText(this,"Welcome",Toast.LENGTH_LONG).show();
String txte=i.getStringExtra("txte");
TextView tv=new TextView(this);
Button btnBack = new Button(this);
btnBack.setText("Back");
tv.setText("Welcome "+txte+" !");
tv.setTextColor(Color.rgb(255, 255, 100));
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);
btnBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//setContentView(R.layout.main);
finish();
}
});
}
}