Write an application which passes the data from one activity to another and display that data



Here we have developed an application which passes the data from one activity to another and shows that data on the screen. The activity_main.xml file is as follows:

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

<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="46dp"
android:layout_marginTop="50dp"
android:text="Enter the name"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/tvMessage"
android:layout_marginTop="30dp"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="OK" />

</RelativeLayout>

In this layout file we have used the relative layout and use one edit text for input name and one button for sending data to other activity. And other xml file is as follows:

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

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to other activity" />

<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="63dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

In this layout file we have used the relative layout and used two text view, 1st displays the Welcome to other activity message and 2nd displays the data coming from the first activity. The MainActivity.java file is as follows:

package com.example.activitydatatransfer;

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

public class MainActivity extends Activity {
EditText name;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name = (EditText)findViewById(R.id.etName);
submit = (Button) findViewById(R.id.btnSubmit);

submit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

String n = name.getText().toString();
Intent intent = new Intent(MainActivity.this,Second.class);
intent.putExtra("name",n);
startActivity(intent);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

In this java file we have used the following packages:

android

In this file we have declared the following variables:
android

Here we have declared one name variable of type EditText and one variable submit of type Button. Now we have to register the controls inside the onCretate() method as follows:
android

Here we have registered controls using their ids defined in the xml file. After that we have to perform the action on button using the setOnClickListener() method as follows:
android

Inside this method we have overloaded the onClick() method. Inside this method, we have used getText() method to get the input data by user and convert this data into string format by using the toString() method and assigned it to string type variable n. After that we have created an intent object and pass the current activity context as first argument and pass the class file of Second activity as second parameter. After that we used intent.putExtra(name,n) method, here we passed two arguments,1st is used for give unique name for identification in other activity, and 2nd is the input data of edit text. After that we have called the startActivity(intent) method and pass the intent object; this will start the defined other activity.
The Second.java file is as follows:

package com.example.activitydatatransfer;

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

public class Second extends Activity {
TextView message;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

message = (TextView) findViewById(R.id.tvMessage);
String getMessage = getIntent().getExtras().getString("name");
message.setText("Welcome : "+getMessage);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}

}

In this java file we have used the following packages:
android

In this java file we have declared following variables:
android

Here we have declared variable message of type TextView that display the message coming from the main activity.
After that we have to register this control as follows:
android

Inside this, we have registered the control using id defined in the xml file. Here we have used the getIntent().getExtras().getString(name) method that holds the data coming from the main activity based on the defined unique name in the putExtra() methods first parameter in the main activity and here it is assigned to string type variable getMessage. After that we have used the setText() method to display the message coming from main activity to the screen.
The output is as follows:
android

When input
android

when click on button