Pass Data from one activity to another

Steps

  • 1.Create a new android project with two activities ActivityA and ActivityB and corresponding xml layouts activity_a.xml and activity_b.xml
  • 2.Create an EditText in activity_a to take user input which will be passed to ActivityB
  • 3.On button click get the user input from the EditText and add it to the intent used for starting ActivityB
  • 4.In ActivityB retrieve and display the value passed from ActivityA

1.Create a new android project with two activities ActivityA and ActivityB and corresponding xml layouts activity_a.xml and activity_b.xml
In android studio go to File->New->New Project. Learn more

2.Create an EditText in activity_a to take user input which will be passed to ActivityB

   <EditText
        android:gravity="center"
        android:hint="Enter your name"
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

Now the activity_a.xml looks like below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    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"
    android:gravity="center"
    android:orientation="vertical"
    >
    <TextView
        android:textSize="25sp"
        android:text="Activity A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_marginBottom="20dp"
        android:textStyle="bold"
        android:textColor="@color/gray"
        android:textSize="12sp"
        android:text="Enter your name below an hit the button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <EditText
        android:gravity="center"
        android:hint="Enter your name"
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:layout_marginTop="20dp"
        android:onClick="loadActivityB"
        android:textAllCaps="false"
        android:padding="10dp"
        android:textColor="@android:color/white"
        android:background="@color/btn_bg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Activity B" />
</LinearLayout>

And here is the activity_b.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_b"
    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"
    android:gravity="center"
    android:orientation="vertical"
    >
    <TextView
        android:textStyle="bold"
        android:textColor="@color/gray"
        android:textSize="12sp"
        android:text="Welcome to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:textSize="25sp"
        android:text="Activity B"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"/>
    <TextView
        android:text="Hello World"
        android:id="@+id/textview_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></TextView>
</LinearLayout>

 

 

 

 

 

 

3. On button click get the user input from the EditText and add it to the intent used for starting ActivityB
To pass a value to the ActivityB we have to add it to the intent that will be used to start ActivityB as shown below:

 Intent intent = new Intent(this, ActivityB.class);
 intent.putExtra("name",name); //name is the string variable holding the user input
 startActivity(intent);

Here intent.putExtra(“name”,name) has been supplied to parameters; first one is the name of the value to be passed (this will be used by the recieving activity to extract the value) and the value itself . The complete code for ActivityA is shown below:

package com.gitanjal.passdataactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ActivityA extends AppCompatActivity {

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

        /*Access the button and set on click listener*/
        Button btnLoadActivityB= (Button) findViewById(R.id.button_load_b);
        btnLoadActivityB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                loadActivityB();
            }
        });
    }

    /*
    * Check if the EditText has some text, if yes pass it to ActivityB else display a message
    * */
    private void loadActivityB()
    {
        /*Access the EditText from layout and get the user input*/
        EditText etName= (EditText) findViewById(R.id.et_name);
        String name=etName.getText().toString();

        /*check if empty*/
        if(name.equals(""))
        {
            /*Do not load ActivityB if */
            Toast.makeText(this,"Please enter your name",Toast.LENGTH_LONG).show();
        }
        else {
            Intent intent = new Intent(this, ActivityB.class);
            intent.putExtra("name",name);
            startActivity(intent);
        }


    }

}

4.In ActivityB retrieve and display the value passed from ActivityA
We have to access the intent that started this activity using getIntent() method of the Activity class.
And form this Intent object we shall retrieve the string sent by ActivityA using getStringExtra(String name) method of the Intent class.

Intent intent=getIntent();
String name=intent.getStringExtra(“name”);

package com.gitanjal.passdataactivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class ActivityB extends AppCompatActivity {

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

        /*get the value passed by ActivityA from intent*/
        Intent intent=getIntent();
        String name=intent.getStringExtra("name");


        /*Display the name*/
        TextView tvName= (TextView) findViewById(R.id.textview_name);
        tvName.setText(name);
    }
}

Leave a Reply

Your email address will not be published.