Button Click Open Gmail app and send email android kotlin example

29-Dec-2024

Button Click Open Gmail app and send email android kotlin example


Output: 
















Give the recipient email, and subject body, and click on the  send email button it will open your Gmail app and set data for sending the email


process:


1/Modify the activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:orientation="vertical"
android:gravity="center"
android:paddingHorizontal="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/recipientEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Recipient Email"
android:inputType="textEmailAddress"
android:padding="12dp" />

<EditText
android:id="@+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"
android:padding="12dp"
android:layout_marginTop="8dp" />

<EditText
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Body"
android:inputType="textMultiLine"
android:minLines="5"
android:maxLines="5"
android:gravity="top"
android:padding="12dp"
android:layout_marginTop="8dp" />

<Button
android:id="@+id/btnSendEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email"
android:layout_marginTop="16dp" />
</LinearLayout>



2 /Modify the MainActivity.kt



package com.zissofworks.openappandsendgmail

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
private lateinit var recipientEmail: EditText
private lateinit var subject: EditText
private lateinit var body: EditText
private lateinit var btnSendEmail: Button

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

recipientEmail = findViewById(R.id.recipientEmail)
subject = findViewById(R.id.subject)
body = findViewById(R.id.body)
btnSendEmail = findViewById(R.id.btnSendEmail)

btnSendEmail.setOnClickListener {
val recipient = recipientEmail.text.toString().trim()
val subject = subject.text.toString().trim()
val body = body.text.toString().trim()

if (recipient.isEmpty()) {
recipientEmail.error = "Please provide recipient email"
}
else if(subject.isEmpty()){
recipientEmail.error = "Please provide subject"
}
else if(body.isEmpty()){
recipientEmail.error = "Please provide body"
}
else sendEmail(recipient, subject, body)
}
}

private fun sendEmail(recipient: String, subject: String, body: String) {
val intent = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
putExtra(Intent.EXTRA_SUBJECT, subject as CharSequence)
putExtra(Intent.EXTRA_TEXT, body)
type = "message/rfc822"
setPackage("com.google.android.gm")

}
startActivity(Intent.createChooser(intent, "Choose email client:"))
}
}



Run the project and show the result.


Comments