How to Get Extra Data From Intent in Android?
Last Updated :
17 Jul, 2022
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, we will take a look at How to get extra data to send from one activity into another activity. A sample video is given below to get an idea about what we are going to do in this article.’
Note: This Android article covered 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 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 below code to it. 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 code below. 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/idTVHeading"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_above = "@id/idTVMsg"
android:layout_centerInParent = "true"
android:layout_margin = "20dp"
android:gravity = "center"
android:padding = "10dp"
android:text = "Getting Extra Data on below line"
android:textAlignment = "center"
android:textColor = "@color/black"
android:textSize = "20sp"
android:textStyle = "bold" />
< 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:
Share your thoughts in the comments
Please Login to comment...