Reputation: 3171
I am trying to open up Gmail Compose screen when a button is clicked in my Android App. Do I need some API key for this from Google? or what do I need to do in my button onClickListener?
Any kind of insight is much appreciated.
Upvotes: 30
Views: 66875
Reputation: 4413
try {
Intent intent = new Intent (Intent.ACTION_VIEW , Uri.parse("mailto:" + "your_email"));
intent.putExtra(Intent.EXTRA_SUBJECT, "your_subject");
intent.putExtra(Intent.EXTRA_TEXT, "your_text");
startActivity(intent);
} catch (ActivityNotFoundException e){
//TODO smth
}
Upvotes: 44
Reputation: 171
Just Place the set of code in your click event/trigger event and it will directly navigate you to the native gmail application with all the details pre-filled.
All the email attributes/details are there in the set of code below(Comments added).
Intent intent = new Intent(Intent.ACTION_SEND);
String[] recipients = {"[email protected]"};//Add multiple recipients here
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, "Mail Subject"); //Add Mail Subject
intent.putExtra(Intent.EXTRA_TEXT, "Enter your mail body here...");//Add mail body
intent.putExtra(Intent.EXTRA_CC, "[email protected]");//Add CC emailid's if any
intent.putExtra(Intent.EXTRA_BCC, "[email protected]");//Add BCC email id if any
intent.setType("text/html");
intent.setPackage("com.google.android.gm");//Added Gmail Package to forcefully open Gmail App
startActivity(Intent.createChooser(intent, "Send mail"));
#HAPPY_CODING
Upvotes: 12
Reputation: 11
You can use Simple Intent.ACTION_SEND
intent
set Intent.EXTRA_EMAIL
for array of emails
set Intent.EXTRA_SUBJECT
for subject line in email composer
Explore more EXTRA options available here -> https://developer.android.com/guide/components/intents-common#Email
Here's a quick code snippet
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback");
if (intent.resolveActivity(ctx.getPackageManager()) != null) {
startActivity(intent);
}
Upvotes: 1
Reputation: 2151
You just place below code inside your click event. Will open directly gmail as compose mode, Output screenshot attached below.
Happy coding :-)
code :
Intent intent=new Intent(Intent.ACTION_SEND);
String[] recipients={"[email protected]"};
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT,"Subject text here...");
intent.putExtra(Intent.EXTRA_TEXT,"Body of the content here...");
intent.putExtra(Intent.EXTRA_CC,"[email protected]");
intent.setType("text/html");
intent.setPackage("com.google.android.gm");
startActivity(Intent.createChooser(intent, "Send mail"));
Output :
Upvotes: 22
Reputation: 51
Intent intent = new Intent(Intent.ACTION_SEND).setType("text/plain")
.putExtra(Intent.EXTRA_EMAIL, new String[]{emails});
List<ResolveInfo> matches = activity.getPackageManager().queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (ResolveInfo info : matches) {
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail")) {
best = info;
}
}
if (best != null) {
intent.setClassName(best.activityInfo.packageName,best.activityInfo.name);
}
activity.startActivity(intent);
Upvotes: 0
Reputation: 575
<TextView
android:id="@+id/EmailId"
android:linksClickable="true"
android:autoLink="email"
android:text="[email protected]"
/>
This is the best method to send email on click of textView.
Upvotes: 2
Reputation: 51
public static void openGmail(Activity activity,String[] email, String subject, String content) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, email);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);
final PackageManager pm = activity.getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
ResolveInfo best = null;
for(final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") || info.activityInfo.name.toLowerCase().contains("gmail"))
best = info;
if (best != null)
emailIntent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
activity.startActivity(emailIntent);
}
Upvotes: 3
Reputation: 1083
This code will directly start the gmail application to send an email.
I found out using this post that the important part here is to find the "packageName" and the "activityInfo.name"
I wanted to only use gmail without a chooser. Note that the package name is hard coded so if Gmail changes its packagename it won't work any more.
The key to this was the setComponent where the first param is the package name and the second param is the activityInfo name.
But like i said use with caution, I repeat myself; if the user doesn't have the gmail app installed or gmail changes its package name or Activty name to send an email this (hard)code will break. Thy have been warned ;)
Here is my code
Intent myIntent = new Intent(Intent.ACTION_SEND);
PackageManager pm = getPackageManager();
Intent tempIntent = new Intent(Intent.ACTION_SEND);
tempIntent.setType("*/*");
List<ResolveInfo> resInfo = pm.queryIntentActivities(tempIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
if (ri.activityInfo.packageName.contains("android.gm")) {
myIntent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
myIntent.setType("message/rfc822");
myIntent.putExtra(Intent.EXTRA_TEXT, "extra text");
myIntent.putExtra(Intent.EXTRA_SUBJECT, "Extra subject");
myIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("uri://your/uri/string");
}
}
startActivity(myIntent);
Upvotes: 1
Reputation: 2526
As JeffC pointed out, it is easy to essentially tell Android that you want to send something email-like and have Android give users a list of choices, which will probably include GMail. If you specifically want GMail, you have to be a bit cleverer. (Note that the correct MIME type is actually "text/plain", not "plain/text". Do to an implementation oddity, GMail seems to be the only activity which responds to the latter, but this isn't a behavior I would count on.)
The following App demonstrates the principle you can follow: actually examine all of the activities which say they can handle your SEND intent and see if any of them look like GMail.
package com.stackoverflow.beekeeper;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import java.util.List;
public class StackOverflowTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm") ||
info.activityInfo.name.toLowerCase().contains("gmail")) best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName, best.activityInfo.name);
startActivity(intent);
}
}
Upvotes: 47
Reputation: 67
if you don't get anything in this line
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
then replace this line with
final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 1);
Upvotes: 0
Reputation: 1549
I don't know that you can specifically launch gmail. Have you tried this in your onClickListener
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
startActivity(emailIntent);
You can find more details here: Email android intent
Upvotes: 40