Home » Android » Android Applications » How to compose Gmail on button Click in Android Java & Kotlin ?

How to compose Gmail on button Click in Android Java & Kotlin ?

If you are developing an android application where you want to have a mechanism wherein your users can reach to you in a button click over Email, you can do that by creating a button and when your user clicks on that button, Android standard Gmail will open with predefined “To” and “Subject” and user just has to enter email description and click on send to reach to you.

This can be done as below, and you will need to modify your code something similar…

Open Gmail Compose code in Kotlin

        myView!!.btn_email.setOnClickListener {
            val intent = Intent().apply {
                action = Intent.ACTION_SEND
                data = Uri.parse("mailto:")
                type = "text/plain"
                putExtra(Intent.EXTRA_EMAIL, "to@email.com")
                putExtra(Intent.EXTRA_SUBJECT, "Subject of Email")
            if (intent.resolveActivity(activity!!.packageManager) != null) {
            } else {
                Log.d(TAG, "No app available to send email.")

As you can see above, we have used two variables of Intent, EXTRA_EMAIL which is A String array holding e-mail addresses that should be delivered to (currently we are using only one email id in to) and EXTRA_SUBJECT which is a constant string holding the desired subject line of a message. The other supported variables are,

  • EXTRA_CC which is A String[] holding e-mail addresses that should be carbon copied
  • EXTRA_BCC which is A String[] holding e-mail addresses that should be blind carbon copied.
  • EXTRA_TEXT which you can use to write body of email.

Also, note here that we have set the package name to “com.google.android.gm” which is Google Gmail package name, if we don’t set the package name android opens all possible apps it may find suitable to send an email and show it to you, but if we set package name to gmail, it will open only Gmail and show the compose email window.

Open Gmail Compose code in JAVA

Intent intent = new Intent(Intent.ACTION_SEND);
String[] emails_in_to={"to@email.com"};
intent.putExtra(Intent.EXTRA_EMAIL, emails_in_to );
intent.putExtra(Intent.EXTRA_SUBJECT,"Your Email's subject");
intent.putExtra(Intent.EXTRA_TEXT, "Your Email's predefined Body");

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment