Pr.Pg Next Pg

Passing Data between Activities tutorials

  • In this tutorial we will illustrate how to pass data between two activities.

  • We will explain the concept by building an application in which there will be two activities, send and receive.

 

The XML Files

  • We will have a EditText, Radio buttons and a button to send the data to the next activity in the first layout.

  • In the second layout we will display the option selected in the first layout and display it.

  • The snippets show the layouts of send.xml and receive.xml respectively.

 

 

send.xml

 

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

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TextView

android:layout_gravity="center" android:gravity="center" android:id="@+id/tvQuestion"

android:layout_width="match_parent" android:layout_height="wrap_content"

android:textSize="25sp" android:text="Android is the best OS?" />

 

<RadioGroup android:id="@+id/radioGroup1"

android:layout_width="wrap_content" android:layout_height="wrap_content">

<RadioButton android:id="@+id/rbYes" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Yes" />

<RadioButton android:id="@+id/rbNo" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="No" />

<RadioButton android:id="@+id/rbCantSay" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="Can't Say" />

</RadioGroup>

 

<Button

android:layout_gravity="center" android:gravity="center"

android:id="@+id/bSend" android:layout_width="wrap_content"

android:layout_height="50sp" android:text="Send Data" />

</LinearLayout>

 

 

 

receive.xml

 

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

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

 

<TextView

android:id="@+id/tvResult"

android:gravity="center"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceLarge" />

 

</LinearLayout>

 

Send.java

  • We then build the Send.java file.

 

 

 

package com.example.helloandroid;

 

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.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

import android.widget.TextView;

 

public class Send extends Activity implements OnClickListener,OnCheckedChangeListener {

 

TextView question;

RadioGroup rg;

Button sendData;

String ans;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.send);

initializeVars();

}

private void initializeVars() {

// TODO Auto-generated method stub

sendData = (Button)findViewById(R.id.bSend);

rg = (RadioGroup)findViewById(R.id.radioGroup1);

question = (TextView)findViewById(R.id.tvQuestion);

sendData.setOnClickListener(this);

rg.setOnCheckedChangeListener(this);

}

@Override

public void onCheckedChanged(RadioGroup arg0, int b_id) {

// TODO Auto-generated method stub

switch(b_id){

case R.id.rbYes:

ans = "Yes";

break;

case R.id.rbNo:

ans="No";

break;

case R.id.rbCantSay:

ans = "Can't Say";

break;

}

}

 

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

if(rg.getCheckedRadioButtonId()!=-1){

Bundle container = new Bundle();

Intent in = new Intent(Send.this,Receive.class);

container.putString("dataSend",ans);

in.putExtras(container);

startActivity(in);

}

else {

}

}

@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

finish();

}

}

 

  • We initializse variables as shown above. This is similar to what we have done in our previous examples.

  • We have also set the view to out layout i.e send.xml.

  • The variables initialized are the Button, RadioButtons and the TextViews.

  • We also make calls the OnClickListener() for the Button and OnCheckedChangeListener() for the Radio Button. This methods we will define as shown.

  • In the onCheckedChanged() method we set the string according to which radio button was clicked.

  • Initially when the activity starts none of the radio buttons are clicked.

  • You can however change the setting in XML Layout by using the android:checked=”true” attribute.

  • We use the IDs of the radio buttons to refer which button was clicked.

  • We now discuss the onClick() method for the Button.

  • In the onClick() method we have to first check if any of the Radio Button has been clicked so that data can be passed to another activity. If we do not check our application might “Force Close” in such situation.

  • The method getCheckedRadioButtonId() returns the Id of the radio button checked. If no such radio button is there then it returns -1.

  • If a Radio Button is checked, then we make a Bundle named container and then to that container we add the string ansin which we have saved the text of the Radio Button clicked.

  • For this purpose we use the putString() method of the Bundle Class.

  • This method takes in as the parameter a label for the string which will be used as the key by the receiving activity to find this string and the actual string

  • Bundle can be used contain data of various types.

  • Finally we add our bundle i.e container, to the Intent declared.

  • For this purpose we use the putExtras() method.

  • Finally we start the activity using the startActivity() method.

  • We also add to the our class the onPause() method.

  • This is done so that our activity will finish as soon as the new activity is started.

  • For this purpose we make a call to the finish() method.

  •  

Receive.java

 

  • We now pay attention to the Receive activity.

 

Receive.java

 

package com.example.helloandroid;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

 

public class Receive extends Activity{

TextView result;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.receive);

result = (TextView)findViewById(R.id.tvResult);

Bundle getData = getIntent().getExtras();

String answer = getData.getString("dataSend");

result.setText(answer);

}

 

}

 

 

  • The purpose of this activity is basically to display the data sent by the previous activity.

  • We have a Text View which will display the text of the radio button which was clicked in the previous activity.

  • In this class we have declared the TextView variable named result.

  • We have set the layout of the activity as receive which we have already defined.

  • We then create a Bundle which will get the Extras from the Intent which was called the activity.

  • The getExtras() method will return the Bundle, which will then be assigned to the Bundle getData.

  • We then extract the string from the bundle using the getString() method using the key set by the sending activity.

  • Finally we set the text of the TextView as the string extracted and the text will be displayed.

  • We also have to add our activity in the AndroidManifest.xml.

  • We can now either add the class name in our MenuList activity and launch it from there or we can just set the Launcher in the AndroidManifest.xml file to be this activity.

  • Make sure you set the action name correctly.

  • The screenshots below will show how the application actually works.

 

 

Pr.Pg border                                              Next Pg