Android – Pass Data From Activity to Activity

android
android






We can pass data from one activity(page) to another Activity.

Live Demo

 

Step 1

Create a android project with blank activity.

Step 2

Add two more activity

Right Click on Layout ->Activity ->Blank Activity

 

Create a Blank Project
Create a Blank Project
Android Blank Activity
Android Blank Activity

Click Finish.

Follow the same steps and create another Activity

3 Activity
3 Activity

Step 3

 

Design The Layout

Activity_main.xml

first acitivt
first Activity
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Personal Details"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:id="@+id/txtName"
android:layout_below="@+id/textView"
android:layout_marginTop="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Father Name"
android:id="@+id/txtFatherName"
android:layout_below="@+id/txtName"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mother Name"
android:id="@+id/txtMotherName"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/btnNext"
android:layout_marginTop="83dp"
android:layout_below="@+id/txtMotherName"
android:layout_centerHorizontal="true"
android:onClick="mainActivityNext"/>

</RelativeLayout>

Activity_activity2.xml

Second Activity
Second Activity
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="javaant.com.pass_data_activity_to_activity.Activity2">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Exp Details"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Company Name"
android:id="@+id/txtCompany"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Loacation"
android:id="@+id/txtLocation"
android:layout_below="@+id/txtCompany"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/btnExpNext"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="nextButton"/>
</RelativeLayout>

 

Activity_activity3.xml

Third Activity
Third Activity
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="javaant.com.pass_data_activity_to_activity.Activity3"
android:id="@+id/activity3">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Summary "
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Personal Details"
android:id="@+id/textView4"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="@+id/txtSummaryName"
android:layout_below="@+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Father name"
android:id="@+id/txtSummaryFname"
android:layout_below="@+id/txtSummaryName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="42dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mother Name"
android:id="@+id/txtSummaryMname"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Exp Details"
android:id="@+id/textView8"
android:layout_marginTop="42dp"
android:layout_below="@+id/txtSummaryMname"
android:layout_alignLeft="@+id/textView3"
android:layout_alignStart="@+id/textView3" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Company Name"
android:id="@+id/txtSummaryCompany"
android:layout_below="@+id/textView8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="44dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:id="@+id/txtSummaryLocation"
android:layout_marginTop="46dp"
android:layout_below="@+id/txtSummaryCompany"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

MainActivity.java

package javaant.com.pass_data_activity_to_activity;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

public class MainActivity extends Activity {

EditText name;

EditText fName;

EditText mName;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

name= (EditText) findViewById(R.id.txtName);

fName= (EditText) findViewById(R.id.txtFatherName);

mName= (EditText) findViewById(R.id.txtMotherName);

}

public void mainActivityNext(View v){

//Call the 2nd activity on click of next

Intent myIntent = new Intent(this, Activity2.class);

myIntent.putExtra("name", name.getText());

myIntent.putExtra("fname", fName.getText());

myIntent.putExtra("mname", mName.getText());

startActivity(myIntent);

}

}

 

Activity2.java

package javaant.com.pass_data_activity_to_activity;

 

import android.app.Activity;

import android.content.Intent;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.EditText;

public class Activity2 extends Activity {

EditText companyName;

EditText location;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_activity2);

companyName= (EditText) findViewById(R.id.txtCompany);

location= (EditText) findViewById(R.id.txtLocation);

}

public void nextButton(View v){

Intent myIntent = new Intent(this, Activity3.class);

Intent intent = getIntent();

/////////////////from 1st page

String fName = intent.getStringExtra("fname");

String name = intent.getStringExtra("name");

String mName = intent.getStringExtra("mname");

 

//////////////////pass new data to 3rd page page

myIntent.putExtra("company", companyName.getText());

myIntent.putExtra("location", location.getText());

////////////////////1st page data send to 3rd page

myIntent.putExtra("fname", fName);

myIntent.putExtra("name", name);

myIntent.putExtra("mname", mName);

startActivity(myIntent);

}

}

Activity3.java

package javaant.com.pass_data_activity_to_activity;

import android.app.Activity;

import android.content.Intent;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.EditText;

import android.widget.TextView;

public class Activity3 extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_activity3);

//////////////////get all the data

Intent intent = getIntent();

String fname=intent.getStringExtra("fname");

String name=intent.getStringExtra("name");

String mname=intent.getStringExtra("mname");

String company=intent.getStringExtra("company");

String location=intent.getStringExtra("location");

 

TextView txtname= (TextView) findViewById(R.id.txtSummaryName);

TextView txtFname=(TextView) findViewById(R.id.txtSummaryFname);

TextView txtMname=(TextView) findViewById(R.id.txtSummaryMname);

TextView txtCompany=(TextView) findViewById(R.id.txtSummaryCompany);

TextView txtLocation=(TextView) findViewById(R.id.txtSummaryLocation);

 

 

////////////////////Set the value

txtname.setText(name);

txtFname.setText(fname);

txtMname.setText(mname);

txtCompany.setText(company);

txtLocation.setText(location);

}

}

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="javaant.com.pass_data_activity_to_activity" >

 

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".MainActivity"

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=".Activity2"

android:label="@string/title_activity_activity2" >

</activity>

<activity

android:name=".Activity3"

android:label="@string/title_activity_activity3" >

</activity>

</application>

</manifest>

 

 

Profile photo of Nirmal Dhara
About Nirmal Dhara 27 Articles
Java Developer

2 Comments on Android – Pass Data From Activity to Activity

  1. hello.. can i ask how to transfer the answer result for these coding to the other page.. i tried to implement based on your coding.. but i cant seem to get the right result.. below are the coding. thnks

    public void calCarbon(View a) {

    if (a.getId() == R.id.calCarbon_button)
    {
    EditText a1 = (EditText) findViewById(R.id.cartype_edit_text);
    EditText a2 = (EditText) findViewById(R.id.mpg_edit_text);
    EditText a3 = (EditText) findViewById(R.id.distance_edit_text);

    double num1,num2;
    num1 = Double.parseDouble(a1.getText().toString());
    num2 = Double.parseDouble(a2.getText().toString());

    double fuelused = (num2 / num1) * 4.546;
    double perjourney = fuelused * 2.63;
    double peryear = (perjourney * 5 * 47) / 1000;

    • below method for put the data

      public void nextButton(View v){
      Intent myIntent = new Intent(this, Activity3.class);
      Intent intent = getIntent();
      /////////////////from 1st page
      String fName = intent.getStringExtra(“fname”);
      String name = intent.getStringExtra(“name”);
      String mName = intent.getStringExtra(“mname”);

      //////////////////pass new data to 3rd page page
      myIntent.putExtra(“company”, companyName.getText());
      myIntent.putExtra(“location”, location.getText());
      ////////////////////1st page data send to 3rd page
      myIntent.putExtra(“fname”, fName);
      myIntent.putExtra(“name”, name);
      myIntent.putExtra(“mname”, mName);
      startActivity(myIntent);
      }

      below code for

      get the data

      Intent intent = getIntent();
      String fname=intent.getStringExtra(“fname”);
      String name=intent.getStringExtra(“name”);
      String mname=intent.getStringExtra(“mname”);
      String company=intent.getStringExtra(“company”);
      String location=intent.getStringExtra(“location”);

      IF you are not able to solve your problem please read the below link
      https://developer.android.com/reference/android/content/Intent.html

Leave a Reply to Nirmal Dhara Cancel reply

Your email address will not be published.


*


Skip to toolbar