Android Intent using Kotlin

activity_main.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">

   <androidx.appcompat.widget.AppCompatEditText

        android:layout_height="wrap_content"
        android:layout_width="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginHorizontal="50dp"
        android:layout_marginTop="10dp"
       android:textColor="@color/white"
        android:padding="15dp"
        android:id="@+id/editTextName"/>
<androidx.appcompat.widget.AppCompatButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/editTextName"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:id="@+id/buttonSubmit"
    android:layout_marginTop="10dp"
    android:text="Submit"
    />
   <androidx.appcompat.widget.AppCompatButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toBottomOf="@id/buttonSubmit"
      android:id="@+id/buttonEmail"
       android:layout_marginTop="10dp"
       android:text="Send Email"/>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.Kt

package com.example.intents1

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.intents1.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.buttonSubmit.setOnClickListener {
            val message = binding.editTextName.text.toString()
            val intent = Intent(this, MainActivity2::class.java).putExtra("ExtraMessage", message)
            startActivity(intent)
        }
        binding.buttonEmail.setOnClickListener {
            val si = Intent(Intent.ACTION_SEND)
            si.type = "plain/text"

            si.putExtra(Intent.EXTRA_EMAIL, arrayOf("[email protected]"))
            si.putExtra(Intent.EXTRA_SUBJECT, "subject")
            si.putExtra(Intent.EXTRA_TEXT, "mail body")
            startActivity(si)
        }
    }
}

activity_main2.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=".MainActivity2">
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity2.Kt

package com.example.intents1

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.example.intents1.databinding.ActivityMain2Binding

class MainActivity2 : AppCompatActivity() {

    private lateinit var binding: ActivityMain2Binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMain2Binding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
    }

    private fun initUI() {
        val message = intent.getStringExtra("ExtraMessage")
        binding.textView1.text = message
    }
}

Output

Output


Similar Articles