Send Multiple Data From One Activity to Another in Android using Kotlin
Last Updated :
28 Jan, 2022
There are multiple ways for sending multiple data from one Activity to Another in Android, but in this article, we will do this using Bundle. Bundle in android is used to pass data from one activity to another, it takes data in key and value pairs. To understand this concept we will create a simple project in android studio using Kotlin.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to Create a new project in android studio in kotlin.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_icons8_geeksforgeeks"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GeeksForGeeks"
android:textColor="#2FA634"
android:textSize="23sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247" />
<EditText
android:id="@+id/etId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"
android:layout_margin="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.238"
tools:layout_editor_absoluteX="0dp" />
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter age"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etId"
app:layout_constraintVertical_bias="0.037"
tools:layout_editor_absoluteX="10dp" />
<EditText
android:id="@+id/etRoll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Enter gender"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etName"
app:layout_constraintVertical_bias="0.047"
tools:layout_editor_absoluteX="10dp" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send data"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etRoll" />
</androidx.constraintlayout.widget.ConstraintLayout>
Note: We have also included vector images in the drawable folder, if want to use ImageView you also need to add a vector image.
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.ayush.serialize_deserialize
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
class MainActivity : AppCompatActivity() {
lateinit var etId: EditText
lateinit var etName: EditText
lateinit var etRoll: EditText
lateinit var btnSend: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
etId = findViewById(R.id.etId)
etName = findViewById(R.id.etName)
etRoll = findViewById(R.id.etRoll)
btnSend = findViewById(R.id.btnSend)
btnSend.setOnClickListener {
val bundle = Bundle()
bundle.putString("id", etId.text.toString())
bundle.putString("name", etName.text.toString())
bundle.putString("roll", etRoll.text.toString())
val intent = Intent(this, SecondActivity::class.java)
intent.putExtras(bundle)
startActivity(intent)
}
}
}
Step 4: Working with the activity_second.xml file
Navigate to the app > res > layout > activity_second.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:textStyle="bold"
android:textColor="#7CB342"
android:textSize="22sp"/>
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NAME"
android:textStyle="bold"
android:textColor="#7CB342"
android:layout_margin="4dp"
android:textSize="22sp"/>
<TextView
android:id="@+id/tvRoll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROLL"
android:textStyle="bold"
android:textColor="#7CB342"
android:textSize="22sp"/>
</LinearLayout>
Step 5: Working with the SecondActivity.kt file
Go to the SecondActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.ayush.serialize_deserialize
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class SecondActivity : AppCompatActivity() {
lateinit var tvId : TextView
lateinit var tvName : TextView
lateinit var tvRoll : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
tvId = findViewById(R.id.tvId)
tvName = findViewById(R.id.tvName)
tvRoll= findViewById(R.id.tvRoll)
val bundle = intent.extras
if (bundle != null){
tvId.text = "id = ${bundle.getString("id")}"
tvName.text = "Name = ${bundle.getString("name")}"
tvRoll.text = "RollNo = ${bundle.getString("roll")}"
}
}
}
So our app is ready.
Output:
Similar Reads
Android - Pass Parcelable Object From One Activity to Another Using PutExtra In this article, we are going to see how can we pass a Parcelable object from one activity to another activity and display the data on the second activity. To pass a Parcelable object from one activity to another in Android, we can use the putExtra() method of the Intent class and pass in the object
9 min read
How to Send Data From One Activity to Second Activity in Android? This article aims to tell and show how to "Send the data from one activity to second activity using Intent" . In this example, we have two activities, activity_first which are the source activity, and activity_second which is the destination activity. We can send the data using the putExtra() method
7 min read
How to Send Data Back to MainActivity in Android using Kotlin? As there are many methods to send the data, but in this article, we will use startActivityForResult() method. Here we need to launch a child activity using startActivityForResult() method. Then from child activity, we can easily send data back to Main Activity. Example: Note: To implement it in jav
2 min read
How to Send Image File from One Activity to Another Activity? In this article, we are going to send an image from one activity to another. We will be using putExtra to send image and we will be using bundle to get the data sent from the previous activity and then we will be showing the received image. Step by Step Implementation Step 1: Create a New Project To
3 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