Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaHow to Use putExtra() and getExtra() For String Data in Android?

How to Use putExtra() and getExtra() For String Data in Android?

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
    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 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" />
  
    <!--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 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() {
  
    // 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 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: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/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 the 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:

RELATED ARTICLES

Most Popular

Recent Comments