0

I need to show an E-mail chooser with all installed E-mail apps to the user to select an App and check E-mails (not compose). I'm using the below code.

val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(Intent.createChooser(intent, "Open E-mail"))

But it directly opens the default E-mail app without a chooser. How can I show all supported/installed E-mail apps in the chooser?

AndroidDev
  • 35
  • 5

1 Answers1

1

Use ACTION_SEND:


    val intent = Intent(Intent.ACTION_SEND).apply {
        // The intent does not have a URI, so declare the "text/plain" MIME type
        type = "text/plain"
        putExtra(Intent.EXTRA_EMAIL, arrayOf("jan@example.com")) // recipients
        putExtra(Intent.EXTRA_SUBJECT, "Email subject")
        putExtra(Intent.EXTRA_TEXT, "Email message text")
    }
    startActivity(intent)

https://developer.android.com/training/basics/intents/sending


Also your original code has:

startActivity(intent)
startActivity(Intent.createChooser(intent, "Open E-mail")

Drop the duplication


Don't forget to account for the scenario where zero app's match your intent:

try {
    startActivity(intent)
} catch (e: ActivityNotFoundException) {
    // Define what your app should do if no activity can handle the intent.
}

If the system identifies more than one activity that can handle the intent, it displays a dialog (sometimes referred to as the "disambiguation dialog") for the user to select which app to use. If there is only one activity that handles the intent, the system immediately starts it.

createChooser is for when you want the user to potentially select a different app each time. For example when sharing an image, perhaps one time they want to use Signal and another time WhatsApp or Twitter.

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • This doesn't match with my requirement. I need show only E-mail applications in the chooser and user should be able to select an app and check E-mail (open inbox and check E-mail that sent by us) and not compose E-mail. – AndroidDev Mar 13 '23 at 07:37