How to Use putExtra() and getExtra() For String Data in Android?
Last Updated :
03 Jul, 2025
Many times in android applications we have to pass data from one activity to another for performing some operations. There are several different ways that are used to give data from one activity to another activity. In this article, we will specifically take a look at How to use putExtra() and getExtra() for passing and retrieving string data in the android application.
Note: This Android article covers in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
activity_main.xml:
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/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="48dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:hint="Enter your message" />
<!--on below line we are creating a button-->
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pass Data" />
</LinearLayout>
Step 3: Creating a new activity
Navigate to app >java >{package-name}, Right click, New > Empty Activity and name it as MainActivity2 and click on finish to create a new activity.
Step 4: Working with the MainActivity file
Navigate to app > java > {package-name} > MainActivity file and add the code below. Comments are added in the code to get to know in detail.
MainActivity.java
package org.geeksforgeeks.demo;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// Declare views
private EditText messageEditText;
private Button sendMessageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
messageEditText = findViewById(R.id.editText);
sendMessageButton = findViewById(R.id.button);
// Set click listener
sendMessageButton.setOnClickListener(v -> {
// Get user input
String message = messageEditText.getText().toString();
// Create intent to navigate to MainActivity2
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
// Pass data with intent
intent.putExtra("message", message);
// Start the new activity
startActivity(intent);
});
}
}
MainActivity.kt
package org.geeksforgeeks.demo
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// declare variables for views
private lateinit var messageEditText: EditText
private lateinit var sendMessageButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initialize variables
sendMessageButton = findViewById(R.id.button)
messageEditText = findViewById(R.id.editText)
// adding on click listener to button
sendMessageButton.setOnClickListener {
// receiving user input
val message = messageEditText.text.toString()
// creating a new intent to open a new activity
val intent = Intent(this@MainActivity, MainActivity2::class.java)
// passing data to our new activity
intent.putExtra("message", message)
// starting a new activity.
startActivity(intent)
}
}
}
Step 5: Working with the activity_main2.xml file
Navigate to app > res > layout > activity_main2.xml and add the below code to it. Comments are added in the code to get to know in detail.
activity_main2.xml:
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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".MainActivity2">
<!--message text-->
<TextView
android:id="@+id/idTVMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textColor="@color/black"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
Step 6: Working with the MainActivity2 file
Navigate to app > java > {package-name} > MainActivity2 file and add the code below. Comments are added in the code to get to know in detail.
MainActivity2.java
package org.geeksforgeeks.demo;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
// Declare TextView
private TextView messageTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// Initialize TextView
messageTextView = findViewById(R.id.idTVMsg);
// Get message from Intent
String message = getIntent().getStringExtra("message");
// Display message or fallback text
if (message == null || message.trim().isEmpty()) {
messageTextView.setText("No message received");
} else {
messageTextView.setText(message);
}
}
}
MainActivity2.kt
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity2 : AppCompatActivity() {
// declaring variable for text view
private lateinit var messageTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
// initializing variable
messageTextView = findViewById(R.id.idTVMsg)
// receiving data from intent
val message = intent.extras?.getString("message")
// setting received data to text view
if (message.isNullOrBlank()) {
messageTextView.text = "No message received"
} else {
messageTextView.text = message
}
}
}
Output:
Similar Reads
How to Get Extra Data From Intent in Android? There are many parts in android applications where we have to pass data from one activity to another activity for performing some data-related operations on it. For passing and retrieving the data there are several different methods such as passing data through bundles and others. In this article, w
5 min read
How to Get City and State Name from Pincode in Android? Many apps ask users to add the address in their apps. To make this task easy for the users they firstly prompt the user to add Pincode and from that Pincode, they fetch the data such as city and state name. So the user's task is reduced to add city and state names while entering the address. Along w
6 min read
How to Send Data From Activity to Fragment in Android? Prerequisites:Introduction to Activities in AndroidIntroduction to Fragments in AndroidIn Android, a fragment is a portion of the user interface that can be used again and again. Fragment manages its own layout and has its own life cycle. Since fragment is a small portion of the bigger user interfac
5 min read
How to Detect Text Type Automatically in Android? In this article, we are going to implement a feature related to TextView which is very important in every perspective. While using any social Media App or Notes app you may have seen that when we type something, it automatically detect the text type like email, phone, or a URL. Here we are going to
2 min read
How to Use Fast Android Networking Library in Android with Example? In Android, we know multiple networking libraries like Retrofit, Volley. We use these libraries specifically for making networking requests like performing actions on API. But Besides that, there is another library called Fast Networking Library which has a few advantages over other libraries. In th
4 min read
How to Update Data to SQLite Database in Android? We have seen How to Create and Add Data to SQLite Database in Android as well as How to Read Data from SQLite Database in Android. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a look at updating data to SQLite database in
10 min read