Android Kotlin example to pass data from one Activity to another

Introduction :

In this example, we will learn how to pass data from one Activity to another in Android. We will use Kotlin and Android Studio in this exercise. For passing data in Android, we need to use objects of class Intent . Intent is a messaging object. We can use one intent to pass data from one Activity to another Activity, starting service or delivering broadcasts. The intent object takes the start activity and destination activity names. Optionally, we can set data to an intent.

Android Example :

In this example, we will create one Application with two screens or Activities: the first Activity will hold one EditText and one Button. The user will enter text in the EditText and click on the button. On click, we will start the second Activity and pass the string that was entered in the EditText . The second activity will read the data and show it in a TextView .

MainActivity layout :

MainActivity.kt is our first activity name and activity_main.xml is its layout file. Below is the layout xml file content :

<?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">

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="359dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
        android:text="Button"
        android:onClick="onButtonClicked"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

We have one EditText and one Button . On button click, it will invoke the method onButtonClicked in MainActivity.kt . The id of the EditText is editText and the id of the Button is button .

MainActivity.kt :

Below is the source code for MainActivity.kt :

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText

class MainActivity : AppCompatActivity() {
    lateinit var editText: EditText

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        editText = findViewById(R.id.editText)
    }

    fun onButtonClicked(view: View) {
        val i = Intent(this, SecondActivity::class.java).apply {
            putExtra("Data",editText.text.toString())
        }

        startActivity(i)
    }

}
  • Here, we have defined one lateinit EditText variable.
  • Inside onCreate , we are initializing the editText variable.
  • onButtonClicked method is called if the user clicks on the button. It creates one Intent object. We are adding data to this intent object using putExtra method. This method takes data as key-value pairs. The key is “Data” and the data is the text that the user has entered in the edit text. We are converting the text to String using the toString method.

SecondActivity :

The Kotlin file of the SecondActivity is SecondActivity.kt and the layout file is activity_second.xml. One TextView is placed in the center of the layout file as like below :


<?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=".SecondActivity">

    <TextView
        android:id="@+id/secondTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

We can get the data from the intent and assign that value to this TextView :

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val intentValue = intent.getStringExtra("Data")
        findViewById<TextView>(R.id.secondTextView).apply{
            text = intentValue.toString()
        }
    }

}
  • getStringExtra gets the string data that was passed with the intent with key “Data” . Different methods are available similar to this method like getBooleanExtra , getCharExtra etc.

That’s it. If you run this program, it will start the MainActivity and you can enter any text in the EditText as like below : Android intent pass data between activities

If you click on the Button, it will start the SecondActivity and the entered text will be shown : Android intent pass data between activities

You might also like: