Open In App

Send Multiple Data From One Activity to Another in Android using Kotlin

Last Updated : 28 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads