Send Data from One Activity to Another – Activity Intent

intent_androidarena.co.in

Many people told me that you should teach android from the scratch , I told them yes I will and I am , So come to the point here is your tutorial after knowing basic widgets in Android I am going to teach you how to switch between one Activity to another and then send data from one Activity to another. Data or values can be send from one Activity to another using intent.putExtras(key,value).

This is possible via Intent and it is of two type Implicit Intent and Explicit Intent .What’s the difference between Implicit Intent and Explicit Intent ? have a look . Implicit Intent is not specified the component.

Lets take an example – “Java file should call implicit Intent asked the system to perform the service without telling the system  which Java file do the service.

Intent i=new Intent(Intent.ACTION_VIEW);  
i.setData(Uri.parse("http://www.androidarena.com"));  
startActivity(i);

Explicit Intent is something which gives the name of the componet .Lets take an example –

Download Code

Lets start with design part for this project , start with screen1.xml . you have to take two editText ,Two TextView and one Button that’s very simple and  already teach that stuff see my previous tutorials . screen1.xml

<?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:background="#13b586"
    >	
    <TextView android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		android:text="UserName: "
    		android:textColor="#fff"/>
    <EditText android:id="@+id/name" 
    		android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		android:layout_marginBottom="10dip"/>    		
    <TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="Password: "
	    android:textColor="#fff"
	    />
    <EditText android:id="@+id/pass" 
    		android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		android:layout_marginBottom="10dip"/>
    <Button android:id="@+id/btnNextScreen"
     		android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		android:text="Send to Next Screen"
    		android:layout_marginTop="15dip"/>
</LinearLayout>

Now prepare second screen so that if you get values from first Activity then where it will go. so for that you have to take three TextView  and one Button. screen2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 	
  <TextView android:layout_width="fill_parent"
  			android:layout_height="wrap_content"
  			android:text="You Typed ...."
  			android:textSize="25dip"
  			android:gravity="center"
  			android:layout_margin="15dip"/>
  
  <TextView android:id="@+id/txtName"
  			android:layout_width="fill_parent"
  			android:layout_height="wrap_content"
  			android:layout_margin="15dip"
  			android:textSize="18dip"/>
  
  <TextView android:id="@+id/txtpass"
  			android:layout_width="fill_parent"
  			android:layout_height="wrap_content"
  			android:layout_margin="15dip"
  			android:textSize="18dip"/>
  
  <Button android:id="@+id/btnClose"
  			android:layout_width="fill_parent"
  			android:layout_height="wrap_content"
  			android:layout_marginTop="15dip"
  			android:text="Close"/>
  			  
</LinearLayout>

Now have a look on Java part for this application , It is very simple. we are using the concept that we are getting value from EditText and then using i.putExtras(key,getValue from edittext) values can be send . FirstActivity.java

package androidarena.androidswitchviews;

import androidarena.androidswitchviews.R;

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

public class FirstActivity extends Activity {
	// Initializing variables
	EditText inputName;
	EditText inputpass;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen1);		
		
		inputName = (EditText) findViewById(R.id.name);
		inputpass = (EditText) findViewById(R.id.pass);
		Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
		
		btnNextScreen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				//Starting a new Intent
				Intent i = new Intent(getApplicationContext(), SecondActivity.class);
				
				//Sending data to another Activity
				i.putExtra("name", inputName.getText().toString());
				i.putExtra("pass", inputpass.getText().toString());
				
				// starting new activity
				startActivity(i);
				
			}
		});
	}
}

Now you have to code for SecondActivity,java. On the receiver side we are having getStringExtras(key) .

package androidarena.androidswitchviews;

import androidarena.androidswitchviews.R;

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

public class SecondActivity extends Activity {
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        
        TextView txtName = (TextView) findViewById(R.id.txtName);
        TextView txtpass = (TextView) findViewById(R.id.txtpass);
        Button btnClose = (Button) findViewById(R.id.btnClose);
        
        Intent i = getIntent();
        // Receiving the Data via key
        String name = i.getStringExtra("name");
        String pass = i.getStringExtra("pass");
        
        // Displaying Received data
        txtName.setText(name);
        txtpass.setText(pass);
        
       
        btnClose.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				
				finish();
			}
		});
        
    }
}

And yes one more thing please update your AndroidManifest.xml 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="androidarena.androidswitchviews"
      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="androidarena.androidswitchviews.FirstActivity"
                  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="androidarena.androidswitchviews.SecondActivity"></activity>

    </application>
</manifest>

Now Run your project and yeah ! here is you Output – Intent1 intent2 For more updates please do like Our page and post here any Query :p

2 thoughts on “Send Data from One Activity to Another – Activity Intent

  1. Birendra verma

    awesome tutorial .easy to understand the concept .
    sir i have some problem . i want to display student details in listview of another activity when view_all button is clicked .
    sir please help me

    Reply
    1. Android Arena

      Hi Birendra Verma , Thanks keep reading ,
      Before starting just tell me what is your need !!
      As i can understand from question you want to show listview on click of button ?
      So first you have to make sure you have array/arraylist/any collection that can feed your listview , and just inflate it .for more detail visit –
      for custom listview –
      http://goo.gl/x6SigB

      else you can check it on google that simple listview example android .

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *