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
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"
tools:context=".MainActivity">
<!--on below line we are creating
a text for our app-->
<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" />
<!--on below line we are creating
edit text for passing data-->
<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" />
<!--on below line we are creating a button-->
<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() {
// on below line we are creating a variable.
lateinit var msgEdt: EditText
lateinit var passDataBtn: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// on below line we are initializing our variables.
passDataBtn = findViewById(R.id.idBtnPassData)
msgEdt = findViewById(R.id.idEdtMsg)
// on below line we are adding
// click listener for our button
passDataBtn.setOnClickListener {
// on below line we are getting
// data from edit text
val msg = msgEdt.text.toString()
// on below line we are creating a new
// intent to open a new activity.
val i = Intent(this@MainActivity, MainActivity2::class.java)
// on below line we are passing
// data to our new activity.
i.putExtra("message", msg)
// on below line we are
// starting a new activity.
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 {
// on below line we are creating a variable.
private Button passDataBtn;
private EditText msgEdt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing variables with ids.
passDataBtn = findViewById(R.id.idBtnPassData);
msgEdt = findViewById(R.id.idEdtMsg);
// on below line we are adding click listener for our button
passDataBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on below line we are getting data from edit text
String msg = msgEdt.getText().toString();
// on the below line we are creating a new
// intent to open a new activity.
Intent i = new Intent(MainActivity.this, MainActivity2.class);
// on below line we are passing
// data to our new activity.
i.putExtra("message", msg);
// on below line we are
// starting a new activity.
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
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"
tools:context=".MainActivity2">
<!--on below line we are creating
a text for our app-->
<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" />
<!--on below line we are creating
a text for our app-->
<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() {
// on below line we are creating a variable.
lateinit var msgTV: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
// on below line we are initializing our variable.
msgTV = findViewById(R.id.idTVMsg)
// on below line we are setting our message to our text view.
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 {
// on the below line we are creating a variable.
private TextView msgTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line we are initializing variables with ids.
msgTV = findViewById(R.id.idTVMsg);
// on below line we are setting our message to our text view.
msgTV.setText(getIntent().getStringExtra("message"));
}
}
Now run your application to see the output of it.
Output:
Similar Reads
How to Extract Data from PDF file in Android?
PDF is a portable document format that is used to represent data such as images, tables, and many more. Nowadays the use of PDF is increased rapidly in different fields. Many apps have switched overusing PDF files to represent data. So some of the apps have a requirement to extract the data from the
4 min read
How to Read Data from SQLite Database in Android?
In the 1st part of our SQLite database, we have seen How to Create and Add Data to SQLite Database in Android. In that article, we have added data to our SQLite Database. In this article, we will read all this data from the SQLite database and display this data in RecyclerView. What we are going to
12 min read
How to Get Bank Details from IFSC Code in Android?
Many apps such as the E-commerce app requires to accept payments from their users for providing different products or services or for their users. So this apps requires the users to enter bank details for payments. In this payment gateway, users are asked to add their banks IFSC code to get the deta
5 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 Read Data from Realm Database in Android?
In the previous article, we have seen adding data to the realm database in Android. In this article, we will take a look at reading this data from our Realm Database in the Android app. What we are going to build in this article? In this article, we will be simply adding a Button to open a new act
8 min read
How to Set Calendar Event in Android?
In most Android devices, a Calendar is an application that displays a calendar and is most commonly used to set reminders or events. Calendar applications in general are very light-weighted and consume less memory. Calendar has a background thread running that keeps a track of when to alert the user
3 min read
How to Read Data from Firebase Firestore in Android?
In the previous article, we have seen on How to Add Data to Firebase Firestore in Android. This is the continuation of this series. Now we will see How to Read this added data inside our Firebase Firestore. Now we will move towards the implementation of this reading data in Android Firebase. What w
9 min read
How to Update Data in Firebase Firestore in Android?
In the previous article, we have seen on How to Add Data to Firebase Firestore in Android, How to Read the data from Firebase Firestore in Android. Now we will see How to Update this added data inside our Firebase Firestore. Now we will move towards the implementation of this updating data in Androi
10 min read
How to Create a New Fragment in Android Studio?
Android Studio is the official integrated development environment for Google's Android operating system, built on JetBrains' IntelliJ IDEA software and designed specifically for Android development. You can Develop Android App using this. Here, We are going to learn how to create a new Fragment in A
2 min read
How to Read Data from Back4App Database in Android?
We have seen adding data to the Back4App database in the Android app. In this article, we will take a look at reading this data from our database in our Android App. What we are going to build in this article? We will be creating a new screen in the previous application and inside that, we will dis
9 min read