Pass data from one activity to another in android example

Sending Receiving – Get text data between one activity to another using intent.

Intent is also used to send data between two activity using putExtra function. This function is used to hold data with it and like sending a message with post man and there is another function getStringExtra() used to receive the senders data. So here is the complete step by step tutorial for Pass data from one activity to another in android example.

android-project-download-code-button

How to Pass data from one activity to another in android example.

Code for MainActivity.java file.

 package com.senddataintent_android_examples.com;

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

public class MainActivity extends Activity {

 String name, city, state;
 Button DataSending;
 Intent intent;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 DataSending = (Button)findViewById(R.id.button1);
 
 name = "Android-Examples.com";
 city = "Jaipur";
 state = "Rajasthan";
 
 DataSending.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 // Sending data to another activity using Intent.
 intent = new Intent(getApplicationContext(),AnotherActivity.class);
 intent.putExtra("Name", name);
 intent.putExtra("City", city);
 intent.putExtra("State", state);
 startActivity(intent);
 
 }
 });
 
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.senddataintent_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Click Here to Send Data to another activity" />

</RelativeLayout>

Code for AnotherActivity.java file.

 package com.senddataintent_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AnotherActivity extends Activity {

 TextView four,five,six;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_another);
 
 four = (TextView)findViewById(R.id.textView4);
 five = (TextView)findViewById(R.id.textView5);
 six = (TextView)findViewById(R.id.textView6);
 
 //Receiving data from senders activity using Intent.
 four.setText(getIntent().getStringExtra("Name"));
 five.setText(getIntent().getStringExtra("City"));
 six.setText(getIntent().getStringExtra("State"));
 
 }
}

Code for activity_another.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.senddataintent_android_examples.com.AnotherActivity" >

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignRight="@+id/textView1"
 android:layout_below="@+id/textView1"
 android:layout_marginTop="27dp"
 android:text="City"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true"
 android:layout_marginLeft="14dp"
 android:text="Name"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignRight="@+id/textView2"
 android:layout_below="@+id/textView2"
 android:layout_marginTop="29dp"
 android:text="State"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView4"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBottom="@+id/textView1"
 android:layout_marginLeft="45dp"
 android:layout_toRightOf="@+id/textView1"
 android:text="Large Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView5"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBaseline="@+id/textView2"
 android:layout_alignBottom="@+id/textView2"
 android:layout_alignRight="@+id/textView4"
 android:text="Large Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

 <TextView
 android:id="@+id/textView6"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignBottom="@+id/textView3"
 android:layout_alignRight="@+id/textView5"
 android:text="Large Text"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshots:

send data

Pass data from one activity to another in android example

Click Here to download Pass data from one activity to another in android example project.