Wednesday 4 September 2013

How To Pass Simple Intent from one Android Activity to another Activity

Passing Intent Android Passing Intent Android
  1. Here we have written my second tutorial about passing intent from one activity to another activity and also pass data to second screen by using intent.putExtra method.
  2. Intent.putExtra method accept many kind of data like String,int,float,...etc.
  3. Using intent we can pass control to another Activity.Bundle can be used to pass data from one Activity to another Activity.
  4. Below we have written code about login activity to pass username to next Android Activity.
  • Modify your main.xml file by using below code
     <?xml version="1.0" encoding="utf-8"?>  
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:orientation="vertical"  
       android:layout_width="fill_parent"  
       android:layout_height="fill_parent"  
       android:gravity="center|center_vertical"  
       android:background="@color/bgcolor"  
       >  
          <TextView   
            android:text="@string/StrTitle"   
            android:layout_height="wrap_content"   
            android:layout_width="wrap_content"   
            android:padding="15px"   
            android:textSize="25px"/>  
            
          <TextView   
               android:text="Username: "   
               android:id="@+id/textView1"   
               android:layout_height="wrap_content"  
               android:gravity="left"   
               android:layout_width="match_parent">  
          </TextView>  
            
          <EditText   
               android:id="@+id/editText1"   
               android:layout_width="match_parent"   
               android:layout_height="wrap_content"   
               android:hint="Username"   
               android:paddingBottom="5px">  
            <requestFocus></requestFocus>  
          </EditText>  
       
          <TextView   
               android:gravity="left|fill"  
               android:layout_width="match_parent"   
               android:text="Password: "   
               android:layout_height="wrap_content"   
               android:id="@+id/textView2"   
               android:paddingTop="5px">  
          </TextView>  
       
          <EditText   
               android:id="@+id/editText2"   
               android:layout_width="match_parent"   
               android:layout_height="wrap_content"   
               android:inputType="textPassword"   
               android:hint="Password">  
          </EditText>  
            
          <Button   
               android:text="Login me"   
               android:id="@+id/button1"   
               android:layout_width="wrap_content"   
               android:layout_height="wrap_content"   
               android:layout_marginTop="10px">  
          </Button>  
     </LinearLayout>  
       
    
  • MainActivity.java
     package com.idroid.passingintent;  
       
     import android.app.Activity;  
     import android.content.Intent;  
     import android.os.Bundle;  
     import android.view.View;  
     import android.view.View.OnClickListener;  
     import android.widget.Button;  
     import android.widget.EditText;  
     import android.widget.Toast;  
       
     public class Prog2Activity extends Activity {  
       /** Called when the activity is first created. */  
       @Override  
       public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
           
         Toast.makeText(this,"Enter username and password : user",Toast.LENGTH_LONG).show();  
         Button login = (Button)findViewById(R.id.button1);  
         final EditText etuname=(EditText)findViewById(R.id.editText1);  
         final EditText etpwd=(EditText)findViewById(R.id.editText2);  
               final String uname="user";  
               final String pwd="user";  
       
                 
               final Intent intent=new Intent(this,NextScreen.class);  
               login.setOnClickListener(new OnClickListener()   
               {  
              @Override  
              public void onClick(View arg0)   
              {  
                   //TODO Auto-generated method stub  
                   if((uname.equals(etuname.getText().toString())) && (pwd.equals(etpwd.getText().toString())))  
                   {    
                        intent.putExtra("uname", uname);  
                        startActivity(intent);  
                   }  
                   else  
                   {  
                        Toast.makeText(Prog2Activity.this,"Username or password incorrect" , Toast.LENGTH_LONG).show();                     
                   }  
                   }  
         });  
       }  
     }  
    
  • Create one new file call NextScreen.jave and modify as given below
     package com.idroid.passingintent;  
     import android.app.Activity;  
     import android.os.Bundle;  
     import android.text.Html;  
     import android.view.Gravity;  
     import android.view.ViewGroup.LayoutParams;  
     import android.widget.TextView;  
     import android.content.Intent;  
     import android.graphics.Color;  
       
     public class NextScreen extends Activity   
     {  
          public void onCreate(Bundle savedInstanceState)   
          {  
         super.onCreate(savedInstanceState);  
           
         Intent intent=getIntent();  
         String uname=intent.getStringExtra("uname");  
           
         TextView tv=new TextView(this);  
         tv.setPadding(20, 20, 10, 10);  
         tv.setBackgroundColor(Color.rgb(00, 66, 99));  
         tv.setTextColor(Color.rgb(255, 255, 255));  
         tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
         tv.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER);  
         tv.setTextSize(25);  
         tv.setText(Html.fromHtml("Welcome <b>"+uname+"</b>"));  
         setContentView(tv);  
          }  
     }  
       
    
  • Now build and run your project in Android simulator provide username and password user you can see next screen with username If you provide wrong username and password it will show you Tost Message.

Download Source Code :Android_Intent_Demo.zip

No comments:

Post a Comment