Skip to content

Instantly share code, notes, and snippets.

@hetang
Last active October 10, 2023 16:37
Show Gist options
  • Save hetang/fccd04ffed62875bd70de0a43b980a8d to your computer and use it in GitHub Desktop.
Save hetang/fccd04ffed62875bd70de0a43b980a8d to your computer and use it in GitHub Desktop.
Open Email inbox or compose view in Android
/**
* This function open email compose view and post HTML body
**/
// val body = Html.fromHtml(fragment.resources.getString(R.string.email_body))
fun navToEmailCompose(email: String, subject: String, body: String) {
val intent = Intent(Intent.ACTION_SENDTO,
Uri.parse("mailto:${email}"))
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject)
intent.putExtra(android.content.Intent.EXTRA_TEXT, body)
startActivity(Intent.createChooser(intent, "Email"))
}
/**
* This function open default email inbox view
**/
fun navToEmail() {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
startActivity(intent)
}
/**
* This function open email app chooser to open inbox view
**/
fun navToEmail() {
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
startActivity(Intent.createChooser(intent, "Email"))
}
@TimoPtr
Copy link

TimoPtr commented Sep 3, 2021

Thanks for your gist, but navToEmail does not work properly since most of the app out there does not use CATEGORY_APP_EMAIL unfortunately (except gmail). I did find a workaround on stackoverflow and create a kotlin version of https://stackoverflow.com/a/69043615/3289338 that also work on Android > 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment