Pass Data From One Activity To Another Activity - Android Studio - Kotlin

In this example, we will pass data from one activity to another activity. See the following example:

activity_main.xml

Add the following code in the activity_main.xml file.

<?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:layout_height="match_parent"
tools:context=".MainActivity">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Activity"
android:gravity="center"
android:layout_gravity="top"
android:textSize="30dp"
android:padding="5dp"
android:background="#8BC34A"
android:textColor="#000000"/>


<EditText
android:id="@+id/edt_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter First Name"/>

<EditText
android:id="@+id/edt_Second_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Second Name"/>

<Button
android:id="@+id/btn_click_to_send"
android:layout_marginTop="10dp"
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="Click To Send"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

MainActivity.kt

Add the following code in the MainActivity.kt class.

package com.study.kotlinkatta

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {

lateinit var edt_first_name: EditText
lateinit var edt_Second_name: EditText
lateinit var btn_click_to_send: Button

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

edt_first_name =findViewById(R.id.edt_first_name)
edt_Second_name =findViewById(R.id.edt_Second_name)
btn_click_to_send =findViewById(R.id.btn_click_to_send)

btn_click_to_send.setOnClickListener {
var str_first_name = edt_first_name.text.toString()
var str_second_name = edt_Second_name.text.toString()

if(str_first_name.equals("") && str_second_name.equals("")) {
Toast.makeText(applicationContext,"Please Enter Details",Toast.LENGTH_SHORT).show()
}else{
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("intent_first_name", str_first_name)
intent.putExtra("intent_second_name", str_second_name)
startActivity(intent)
}
}
}
}

activity_second.xml

Add the following code in the activity_second.xml file.

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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Activity"
android:gravity="center"
android:layout_gravity="top"
android:textSize="30dp"
android:padding="5dp"
android:background="#8BC34A"
android:textColor="#000000"
android:layout_marginBottom="10dp"/>

<TextView
android:id="@+id/txt_first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>

<TextView
android:id="@+id/txt_Second_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"/>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

SecondActivity.kt

Add the following code in the SecondActivity.kt class.

package com.study.kotlinkatta

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

class SecondActivity : AppCompatActivity() {

lateinit var txt_first_name: TextView
lateinit var txt_Second_name: TextView
var str_first_name = ""
var str_second_name = ""

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

val i = intent
val bd = i.extras
if (bd != null) {
str_first_name = bd.getString("intent_first_name")!!
str_second_name = bd.getString("intent_second_name")!!
}

txt_first_name = findViewById(R.id.txt_first_name)
txt_Second_name = findViewById(R.id.txt_Second_name)

txt_first_name.setText("First Name: "+ str_first_name)
txt_Second_name.setText("Second Name: "+ str_second_name)

}
}

Output:





Comments