How to send data from one activity to the second activity

Hey there, today in this article you can see how to send any data from one activity to second activity. In this example, we have two activity first is MainActivity (Source Activity )and the second is Activity2 (Destination Activity).

Methods used to send data:-

  • putExtra():- It is used to send the data, data in key-value pair key is variable name and value can be Int, String, Float, etc.
  • getStringExtra() :- It is used to get the data(key) which is send by above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra().

Example for this tutorial:-

(1). First, you need to create a new Android Application. Now open your acitivity_main.xml file and add one EditText and button. Also, Assign the ID to each component along with other attributes as shown in the code below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <EditText
        android:id="@+id/send_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/send_button"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="115dp"
        android:layout_centerInParent="true"
        android:hint="Input data"
        android:textSize="25dp"
        android:textAlignment="center"
         />

    <Button
        android:id="@+id/send_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="Next" />

</RelativeLayout>

Here is my id, that i used in my code :-

  • Button :- send_button
  • EditText:- send_info

You can see the UI of this code in below image.

send data from one activity to second 1

(2). Now you need to create object both components (EditText, Button) with the help of assigned ID.

Syntax:-

ComponentType object = (ComponentType)findViewById(R.id.IdOfTheComponent);

Code is used to create object of the components :-

Button send= (Button)findViewById(R.id.send_button);
EditText send_text = (EditText) findViewById(R.id.send_info);

(3). In this step, we need to add button on click listener to get data and send data to second activity. For creating a button on click listener you need to follow the below code.

   send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
            }
        });

After writing some words like “new View.OnClickListener” and it will automatically create a click listener of that button like below video.

(4). Now we need to get a text from EditText and store it into any string.

1. First, you need to use the getText() method to get data of EditText into the string like below the below code.

String stringdata= send_text.getText().toString();

2. Now you need to create Intent object MainActivity.java class to Activity2 class. This is done as follows:

getApplicationContext() :- It will fetch the current activity.

Intent intent = new Intent(getApplicationContext(), Activity2.class);

Before that, you need to create a second activity. In my case, I created the second activity name as Activity2.

3. In this you need to pass that string data with intent using key value pair. This is done as follows:

intent.putExtra("data_key",stringdata);
startActivity(intent);

here “stringdata” is our string or also called value and the key is “data_key“. This key will use to getting the stringdata value. you can write anything in the place of key (My key name is “data_key”).

HERE IS THE FULL CODE OF MY FIRST ACTIVITY (MainActivity.java)

package com.walley.smgplaza;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button send= (Button)findViewById(R.id.send_button);
        final EditText send_text = (EditText) findViewById(R.id.send_info);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String stringdata= send_text.getText().toString();
                Intent intent = new Intent(getApplicationContext(), Activity2.class);
                intent.putExtra("data_key",stringdata);
                startActivity(intent);
            }
        });


    }
}

Now you need to create a second activity, if you have already created a new empty activity then no problem.

After creating a new activity, go to the XML file of that activity and add a new TextView in that XML file as below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Activity2">

    <TextView
        android:id="@+id/receivedata"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:layout_centerInParent="true"
        android:textSize="50dp" />


</RelativeLayout>

Last step is open your second activity java file and perform following operation.

1 . Define TextViewe variable, using findViewById() to get the TextView.

TextView receivedata=(TextView) findViewById(R.id.receivedata);

2. Now you need to create the object of getIntent and receive the value in the String type variable by getStringExtra method using mesage_key.

Intent intent=getIntent();
String str=intent.getStringExtra("data_key"); // my key name is data_key

3. The received value set in the TextView object of second Activity XML file.

receivedata.setText(str);

Here is full code of second Activity. If you want to write code then copy this code and simply past it in you java file.

package com.walley.smgplaza;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Activity2 extends AppCompatActivity {

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

        TextView receivedata=(TextView) findViewById(R.id.receivedata);
        Intent intent=getIntent();
        String str=intent.getStringExtra("data_key"); // my key name is data_key
        receivedata.setText(str);
    }
}

Now run your application and enter some data in your EditText and click on Button. After clicking on the send button, you can see your data is available in the second activity.

Output :-

Also Read :- Switch From One Activity to Second Activity On Button Click (Android).

That’s it, your application is ready. If you have any type of query or a problem then comment me on below comment box and also you can contact me on our social media handles.

Instagram Handle :- https://www.instagram.com/smgplaza/

Twitter Handle :- https://twitter.com/smgplaza

Thank you so much for your support,keep visiting to smgplaza for best tips and tricks.