postheadericon Android Intent Example

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


File Name : E2Activity.java



package bsr.e2;

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

public class E2Activity extends Activity {
    /** @author Bipin S Rupadiya , www.gtu-android.blogspot.com
     *
     *  2) 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.
     **/
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
             
        //create an intent
        final Intent intent=new Intent(this,Screen2.class);
     

        //login event
        final Button btnLogin = (Button)findViewById(R.id.cmdLogin);
        btnLogin.setOnClickListener(new Button.OnClickListener()
        {
        @Override
        public void onClick(View v)
        {
        EditText fldu=(EditText)findViewById(R.id.txtusr);
        EditText fldp=(EditText)findViewById(R.id.txtPwd);
        String un= fldu.getText().toString();
       
        if((fldu.getText().toString().equals("bipin"))&&(fldp.getText().toString().equals("rupadiya")))
        {  
        intent.putExtra("uname", fldu.getText().toString());
        startActivity(intent);
        }
        else
        {
        Toast.makeText(E2Activity.this, "sorry  ".concat(un).toString().concat(" you are unauthenticated 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("");        
        }      
        });

    }
 }

File Name : Screen2.java



package bsr.e2;
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("uname");    
        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);
        }      
        });
            
}
}






Blog Archive

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.