How to Send Data From One Activity to Second Activity in Android?

This article shows how to send data from one activity to the second activity using intent . In this example , we have two activities. Activity_first is source activity, and activity_second  which is the destination activity. PutExtra()  method works by sending data and receiving using this getStringExtra() of android application

There are two methods for sending and receiving data.
  • putExtra(): This method is used to send the data. It's work with key and value.
  • getStringExtra(): This method is revised data from source activity.


Step-By-Step Implementation


Step 1: Create a new project in Android or you can use an already created project.

   ​or



Step 2: Create an XML file named acitivity_main_first.xml of the layout folder.


 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="80dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_marginStart="30dp"
android:layout_marginTop="104dp"
android:layout_marginEnd="30dp"
android:hint="Typing...."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

<Button
android:id="@+id/btnsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:text="submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />


 </androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create a kt file named AcitivityMainFirst.kt of the layout folder.
 import android.content.Intent
 import android.os.Bundle
 import android.view.View
 import android.widget.Button
 import android.widget.EditText
 mport androidx.activity.enableEdgeToEdge
 import androidx.appcompat.app.AppCompatActivity


 class AcitivityMainFirst : AppCompatActivity() {


private var send_button: Button? = null
private var send_text: EditText? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_activityone)

//assign object of components
send_button = findViewById(R.id.btnsubmit)
send_text = findViewById(R.id.editText)

//set click listener of button
send_button?.setOnClickListener { v: View? ->
val str = send_text?.getText().toString()
//create object of intent
val intent = Intent(
applicationContext,
MainActivitySecond::class.java
)
//pass data set here
intent.putExtra("receiverdata", str)
startActivity(intent)
   }
}
 }
Step 4:  Create an XML file named acitivity_main_second.xml of the layout folder.
 <?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="80dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/txtData"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_marginStart="30dp"
android:layout_marginTop="104dp"
android:layout_marginEnd="30dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

 </androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Create a kt file named AcitivityMainSecond.kt.xml of the layout folder.

 import android.annotation.SuppressLint
 import android.os.Bundle
 import android.widget.TextView
 import androidx.activity.enableEdgeToEdge
 import androidx.appcompat.app.AppCompatActivity


 class MainActivityTwo : AppCompatActivity() {
var receiverdatat: TextView? = null
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main_two)
receiverdatat = findViewById(R.id.txtData)
val intent = intent
val str = intent.getStringExtra("receiverdatat")
receiverdatat?.setText(str)
  }
 }
Step 5: Output of above example 

Happy coding!







365Bloggy May 21, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive