how to direct open Gmail mail composer in android?


how to direct open Gmail mail composer in android?



I am using the following code. Problem it that when I run this code on device. It open a Dailogbox that hase 3 options for sending a mail. "POP , email , gmail" etc.
By clicking the gamil the composer appear.
I just want to show Gamil mail composer directly. Instead of showing a dailog box for choosing the options. Please help me.


Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");

String subject = "My Subject";

emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

emailIntent.setType("text/html");

String title = "<p align='center'>" + storyTitle + "<br/>" + storyPubDate + "</p>";

String data = "<p> Sent From ABC APP Sent from my Android </p>";

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(title + data));
startActivity(Intent.createChooser(emailIntent, "Email:"));




6 Answers
6



Try this one, Perfect


public void shareToGMail(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);
}





That answer works for me.
– Naresh Sharma
Apr 17 '15 at 3:05





Thank you 1+. It is worked for me.
– Ninja
Jun 17 '17 at 12:23




Not sure about the need for the chooser. This is from one of my apps...


final Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, new String{"jimblackler@gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
activity.startActivity(intent);





Note that setType should be text/plain not plain/text (otherwise you get an ActivityNotFoundException). Thanks for the tip!
– Andrew
Dec 28 '11 at 23:36




Try this code


Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "This is my subject text");
context.startActivity(Intent.createChooser(emailIntent, null));



Ref:
http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO





best solution here
– Guy
Jul 14 '14 at 16:25





liink unavailable
– therealprashant
May 16 '16 at 18:24



You can't bypass this popup programatically. What will happen if a user tries to access the functionality and doesn't have GMail configured?



If you want to bypass is just remove all other email clients so that GMail is the only one that can send/recieve emails. That way the popup will not appear.





yes, There must be not, if Gmail is not configured. But how can I remove all other client from my device?
– Arslan
Apr 26 '11 at 11:32



If you have several mail composer in your android device and you just want Gamil composer start for your request, you have to


emailIntent.setClassName("xxxgamil composer package name xxx", "xxxgmail composer class name xxx");
startActivity(emailIntent);





I found the code. It look like your code 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);
– Arslan
Apr 26 '11 at 11:39





yes.... and Mojo's link is also useful.
– XC.
Apr 27 '11 at 3:36






Done. But after sending the mail. It again show to dialog box. I don't want to open it any more? Any suggestion?
– Arslan
Apr 27 '11 at 9:02





if you already specified the class name, you should got the composer activity directly, doesn't it?
– XC.
Apr 29 '11 at 7:43



Below code worked for me. This will search for an email client and directly launch the new email composer with the sent values pre-populated. If no email client exists then then that should be caught to avoid crash.



The good think about this solution is that, on back press it takes you directly to your app screen where the email intent was started from.


Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
intent.putExtra(Intent.EXTRA_TEXT, "The message");
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Mail account not configured", Toast.LENGHT_SHORT).show();
}



Thank you!






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift