Pass Data between Activities using intent (Kotlin) - Android Studio Tutorial
In this tutorial i'll show you how to pass data between different activities using intent.
For Example in this tutorial
Activity 1: Input data using EditText and click Save Button to start second activity with the data that was input.
Activity 2: Show the input data of previous activity using Intent.
Step 1: Create a new Project or open new project
Step 2: Create New Activity File>New>Activity>EmptyActivity
Step 3: Code
acitivity_main.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:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:padding="10dp" tools:context=".MainActivity"> <EditText android:id="@+id/nameEt" android:hint="Enter Name" android:inputType="text" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/emailEt" android:hint="Enter Email" android:inputType="textEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/phoneEt" android:hint="Enter Phone" android:inputType="phone" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/saveBtn" android:text="save" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.myapplication import android.content.Intent import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.EditText class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val nameEt = findViewById<EditText>(R.id.nameEt) val emailEt = findViewById<EditText>(R.id.emailEt) val phoneEt = findViewById<EditText>(R.id.phoneEt) val saveBtn = findViewById<Button>(R.id.saveBtn) //handle button click saveBtn.setOnClickListener { //get text from edittexts val name = nameEt.text.toString() val email = emailEt.text.toString() val phone = phoneEt.text.toString() //intent to start activity val intent = Intent(this@MainActivity, SecondActivity::class.java) intent.putExtra("Name", name) intent.putExtra("Email", email) intent.putExtra("Phone", phone) startActivity(intent) } } }
activity_second.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:layout_height="match_parent" android:orientation="vertical" android:gravity="center" android:padding="10dp" tools:context=".SecondActivity"> <TextView android:id="@+id/resultTv" android:textSize="30sp" android:textStyle="bold" android:textColor="#000" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
SecondActivity.kt
package com.blogspot.atifsoftwares.myapplication import android.support.v7.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) //get data from intent val intent = intent val name = intent.getStringExtra("Name") val email = intent.getStringExtra("Email") val phone = intent.getStringExtra("Phone") //textview val resultTv = findViewById<TextView>(R.id.resultTv) //setText resultTv.text = "Name: "+name+"\nEmail: "+email+"\nPhone: "+phone } }
Step 4: Run Project
Output
Comments
Post a Comment