How to Use putExtra() and getExtra() For String Data in Android?
Last Updated :
12 Jul, 2022
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.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:id = "@+id/idRLContainer"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity" >
< TextView
android:id = "@+id/idTVHeading"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@id/idEdtMsg"
android:layout_centerInParent = "true"
android:layout_margin = "20dp"
android:gravity = "center"
android:padding = "10dp"
android:text = "Using putExtras() and getExtras() in Android"
android:textAlignment = "center"
android:textColor = "@color/black"
android:textSize = "20sp"
android:textStyle = "bold" />
< EditText
android:id = "@+id/idEdtMsg"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@id/idBtnPassData"
android:layout_margin = "20dp"
android:hint = "Enter your message" />
< Button
android:id = "@+id/idBtnPassData"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:layout_margin = "20dp"
android:text = "Pass Data"
android:textAllCaps = "false" />
</ RelativeLayout >
|
Step 3: Creating a new activity
Navigate to app>java>your app’s 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 > your app’s package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var msgEdt: EditText
lateinit var passDataBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
passDataBtn = findViewById(R.id.idBtnPassData)
msgEdt = findViewById(R.id.idEdtMsg)
passDataBtn.setOnClickListener {
val msg = msgEdt.text.toString()
val i = Intent( this @MainActivity , MainActivity2:: class .java)
i.putExtra( "message" , msg)
startActivity(i)
}
}
}
|
Java
package com.gtappdevelopers.kotlingfgproject;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button passDataBtn;
private EditText msgEdt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passDataBtn = findViewById(R.id.idBtnPassData);
msgEdt = findViewById(R.id.idEdtMsg);
passDataBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = msgEdt.getText().toString();
Intent i = new Intent(MainActivity. this , MainActivity2. class );
i.putExtra( "message" , msg);
startActivity(i);
}
});
}
}
|
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.
XML
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity2" >
< TextView
android:id = "@+id/idTVMsg"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_centerInParent = "true"
android:layout_margin = "20dp"
android:gravity = "center"
android:padding = "10dp"
android:text = "Message"
android:textAlignment = "center"
android:textColor = "@color/black"
android:textSize = "20sp"
android:textStyle = "bold" />
</ RelativeLayout >
|
Step 6: Working with the MainActivity2 file
Navigate to app > java > your app’s package name > MainActivity2 file and add the code below. Comments are added in the code to get to know in detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity2 : AppCompatActivity() {
lateinit var msgTV: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
msgTV = findViewById(R.id.idTVMsg)
msgTV.text = intent.extras?.getString( "message" ) ?: "No message found"
}
}
|
Java
package com.gtappdevelopers.kotlingfgproject;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity2 extends AppCompatActivity {
private TextView msgTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msgTV = findViewById(R.id.idTVMsg);
msgTV.setText(getIntent().getStringExtra( "message" ));
}
}
|
Now run your application to see the output of it.
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...