Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Thursday 18 October 2012

To understand Activity, Intent a. Create sample application with login module.(Check username and password) b. On successful login, go to next screen. And on failing login, alert user using Toast. c. Also pass username to next screen.

Pro2Activity.java
package ps.pro2;

import android.app.Activity;
import android.content.Intent;
import android.view.*;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Pro2Activity extends Activity implements View.OnClickListener 
{
   /**
     *  www.master-gtu.blogspot.com
     *  pankaj sharma(8460479175),
     *  chavda vijay(8460420769) 
     */
 Button btlogin,btclear;
 EditText editusername,editpassword;
 TextView textmsg;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        editusername=(EditText)findViewById(R.id.username);
        editpassword=(EditText)findViewById(R.id.password);
        btlogin=(Button) findViewById(R.id.btlogin);
        btclear=(Button) findViewById(R.id.btclear);
       
        btlogin.setOnClickListener(this);
        btclear.setOnClickListener(this);
    }
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  Button action=(Button) v;
  String name,pass;
  boolean flagusername=false;
  boolean flagpassword=false;
  name=editusername.getText().toString();
  pass=editpassword.getText().toString();
  if(name.equals(""))
  {
   flagusername=true;
  }
  if(pass.equals(""))
  {
   flagpassword=true;
  }
  if(btlogin.getId()==action.getId())
  {
   if(flagusername==false && flagpassword==false)
   {
    if(pass.equals(name+"@123"))
    {
     Toast.makeText(this,"Login success...", Toast.LENGTH_SHORT).show();
     Intent myintent=new Intent(this,home.class);
     myintent.putExtra("name", name);
     this.startActivity(myintent);
    }
    else
     Toast.makeText(this,"Login faild...", Toast.LENGTH_SHORT).show();
   }
   else
   {
    if(flagusername==true)
     Toast.makeText(this,"Pls Enter Username...", Toast.LENGTH_SHORT).show();
    else if(flagpassword==true)
     Toast.makeText(this,"Pls Enter Password...", Toast.LENGTH_SHORT).show();
   }
  }
  else if(btclear.getId()==action.getId())
  {
   if(flagusername==false || flagpassword==false)
   {
    editusername.setText("");
    editpassword.setText("");
    Toast.makeText(this,"Field Cleared...", Toast.LENGTH_SHORT).show();
   }
   else
   {
    Toast.makeText(this,"Already Cleared...", Toast.LENGTH_SHORT).show();
   }
  }
 }
}
home.java
package ps.pro2;

import android.app.Activity;
import android.content.Intent;
//import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class home extends Activity implements OnClickListener {
   /**
     *  www.master-gtu.blogspot.com
     *  pankaj sharma(8460479175),
     *  chavda vijay(8460420769) 
     */
 TextView textmsg;
 Button btback;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        
        
        textmsg=(TextView) findViewById(R.id.textmsg);
        btback=(Button) findViewById(R.id.btback);
        
        btback.setOnClickListener(this);
       
        Intent myintent=getIntent(); 
        textmsg.setText("welcome "+ myintent.getStringExtra("name"));
    }
 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  Intent i=new Intent(this,Pro2Activity.class);
  this.startActivity(i);
 }
}

No comments:

Post a Comment