Open In App

How to Get Extra Data From Intent in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

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: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: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:



Last Updated : 17 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads