How to customize the built-in chooser(dialogue box) of share intent? - android

Is there any way to customize the createChooser of share intent in startActivity(Intent.createChooser(i,"Share via"));?
Showing the apps not in a dialogue box maybe in scroll view as buttons

String urlToShare = "www.google.com"
code to share link twitter
try {
Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
.setType("text/plain")
.setText("Sharing text with image link \n "+urlToShare).setStream(null)
.getIntent()
.setPackage("com.twitter.android");
startActivity(shareIntent);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent intent = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://twitter.com/intent/tweet?text=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
code to share link google plus
try {
Intent shareIntent = ShareCompat.IntentBuilder.from(getParent())
.setType("text/plain")
.setText("Sharing text with image link \n "+urlToShare).setStream(null)
.getIntent()
.setPackage("com.google.android.apps.plus");
startActivity(shareIntent);
} catch (Exception e) {
// If we failed (not native FB app installed), try share through SEND
Intent intent = new Intent(Intent.ACTION_SEND);
String sharerUrl = "https://plus.google.com/share?url=" + urlToShare;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(intent);
}
code to share link whatsapp
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
startActivity(intent);

Yes you can do it by getting the share options as activities and pass them to your adapter, i am posting a sample code
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List activities = this.getPackageManager().queryIntentActivities(sendIntent, 0);
By this you will get share Intents in activities object and then you can convert it to Object Array some thing like this
Objects[] item = activities.toArray();
for( int i=0; i<item.length; i++ ) {
ResolveInfo infoName = (ResolveInfo) items[i];
String name = info.activityInfo.name;
Drawable logo = info.loadIcon(context.getPackageManager());
// Set them to your Views
}
and when your view is clicked and you want to perform the share functionality you will do some thing like this
ResolveInfo info = (ResolveInfo) item(position);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Your Text");
startActivity(intent);

this is what i have done to share link and image to another apps something like this just try
/*
* Method to share either text or URL.
*/
private void shareTextUrl() {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
// Add data to the intent, the receiving app will decide
// what to do with it.
share.putExtra(Intent.EXTRA_SUBJECT, "Title Of The Post");
share.putExtra(Intent.EXTRA_TEXT, "http://www.google.com");
startActivity(Intent.createChooser(share, "Share link!"));
}
/*
* Method to share any image.
*/
private void shareImage() {
Intent share = new Intent(Intent.ACTION_SEND);
// If you want to share a png image only, you can do:
// setType("image/png"); OR for jpeg: setType("image/jpeg");
share.setType("image/*");
// Make sure you put example png image named myImage.png in your
// directory
String imagePath = Environment.getExternalStorageDirectory()
+ "/myImage.png";
File imageFileToShare = new File(imagePath);
Uri uri = Uri.fromFile(imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image!"));
}

Related

how to send image or video to whatsapp status in android programmatically?

How to send image or video to the WhatsApp Status (or story) in android.
we can send an image to contact by using:
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.putExtra(Intent.EXTRA_STREAM, imageURI);
sendIntent.putExtra("jid", "91"+mobile + "#s.whatsapp.net");
sendIntent.putExtra(Intent.EXTRA_TEXT, "whatsapp image caption");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setPackage("com.whatsapp");
sendIntent.setType("image/*");
But how to send it to my whatsapp status?
private void shareToWhatsApp() {
String type = "video/*";
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
Uri uri = FileProvider.getUriForFile(DetailActivity.this, getString(R.string.authority), file);
share.setPackage("com.whatsapp");
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageManager packageManager = getPackageManager();
if (share.resolveActivity(packageManager) != null) {
startActivity(share);
startActivity(Intent.createChooser(share, "Share to"));
} else {
alertForApp(getString(R.string.install_whatsapp), "com.whatsapp");
}
// Broadcast the Intent.
}

How to remove one item for Sending the User to Another App in android?

How can i remove Google+ from the intent?
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
How can i remove Google+ from the intent?
List<Intent> targetIntents = new ArrayList<Intent>();
Intent templateIntent = new Intent(Intent.ACTION_SEND);
templateIntent.setType("text/plain");
List<ResolveInfo> packages = this.getPackageManager().
queryIntentActivities(templateIntent, 0);
// remove desired package
for (ResolveInfo candidate : packages) {
String packageName = candidate.activityInfo.packageName;
if (!packageName.equals("com.google.android.apps.plus")) { //GooglePlus Package name
Intent target = new Intent(android.content.Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TEXT, "Your Text Here");
target.setPackage(packageName);
targetIntents.add(target);
}
}
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Share.");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);

Android Intent directly to Facebook sharing?

Following code works perfectly,
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
but it open a list and from this list I have to select Facebook and then everything is smooth.
But is there any config changes that can open Facebook sharing directly instead of going through the menu.
Try the following code, now the intent will open Facebook directly.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent facebookIntent = getShareIntent("facebook", "subject", "text_or_url");
// subject may not work, but if you have a url place it in text_or_url
if(facebookIntent != null)
targetedShareIntents.add(facebookIntent);
Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "Delen");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooser);
// To precisely get the facebook intent use the following code
private Intent getShareIntent(String type, String subject, String text)
{
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(share, 0);
System.out.println("resinfo: " + resInfo);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) ) {
share.putExtra(Intent.EXTRA_SUBJECT, subject);
share.putExtra(Intent.EXTRA_TEXT, text);
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return null;
return share;
}
return null;
}
Above code is extracted from this stackoverflow-post
this should be possible, if you get first all activities, that supports your intent and then get the correct and do a explicit call. To get all activities use context.getPackageManager.queryIntentActivities(Intent, int). The coding shouldn't be a big problem.
Check this link: PackageManager docu

Android: Share plain text using intent (to all messaging apps)

I'm trying to share some text using an intent:
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(android.content.Intent.EXTRA_TEXT, "TEXT");
and warping with chooser:
startActivity(Intent.createChooser(sms, getResources().getString(R.string.share_using)));
it works! but only for email app.
what I need is a general intent for all messaging app: emails, sms, IM (Whatsapp, Viber, Gmail, SMS...)
tried using android.content.Intent.ACTION_VIEW
and tried using i.setType("vnd.android-dir/mms-sms"); nothing helped...
("vnd.android-dir/mms-sms" shared using sms only!)
Use the code as:
/*Create an ACTION_SEND Intent*/
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
/*This will be the actual content you wish you share.*/
String shareBody = "Here is the share content body";
/*The type of the content is text, obviously.*/
intent.setType("text/plain");
/*Applying information Subject and Body.*/
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_subject));
intent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
/*Fire!*/
startActivity(Intent.createChooser(intent, getString(R.string.share_using)));
New way of doing this would be using ShareCompat.IntentBuilder like so:
// Create and fire off our Intent in one fell swoop
ShareCompat.IntentBuilder
// getActivity() or activity field if within Fragment
.from(this)
// The text that will be shared
.setText(textToShare)
// most general text sharing MIME type
.setType("text/plain")
.setStream(uriToContentThatMatchesTheArgumentOfSetType)
/*
* [OPTIONAL] Designate a URI to share. Your type that
* is set above will have to match the type of data
* that your designating with this URI. Not sure
* exactly what happens if you don't do that, but
* let's not find out.
*
* For example, to share an image, you'd do the following:
* File imageFile = ...;
* Uri uriToImage = ...; // Convert the File to URI
* Intent shareImage = ShareCompat.IntentBuilder.from(activity)
* .setType("image/png")
* .setStream(uriToImage)
* .getIntent();
*/
.setEmailTo(arrayOfStringEmailAddresses)
.setEmailTo(singleStringEmailAddress)
/*
* [OPTIONAL] Designate the email recipients as an array
* of Strings or a single String
*/
.setEmailTo(arrayOfStringEmailAddresses)
.setEmailTo(singleStringEmailAddress)
/*
* [OPTIONAL] Designate the email addresses that will be
* BCC'd on an email as an array of Strings or a single String
*/
.addEmailBcc(arrayOfStringEmailAddresses)
.addEmailBcc(singleStringEmailAddress)
/*
* The title of the chooser that the system will show
* to allow the user to select an app
*/
.setChooserTitle(yourChooserTitle)
.startChooser();
If you have any more questions about using ShareCompat, I highly recommend this great article from Ian Lake, an Android Developer Advocate at Google, for a more complete breakdown of the API. As you'll notice, I borrowed some of this example from that article.
If that article doesn't answer all of your questions, there is always the Javadoc itself for ShareCompat.IntentBuilder on the Android Developers website. I added more to this example of the API's usage on the basis of clemantiano's comment.
This a great example about share with Intents in Android:
* Share with Intents in Android
//Share text:
Intent intent2 = new Intent(); intent2.setAction(Intent.ACTION_SEND);
intent2.setType("text/plain");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(Intent.createChooser(intent2, "Share via"));
//via Email:
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("message/rfc822");
intent2.putExtra(Intent.EXTRA_EMAIL, new String[]{EMAIL1, EMAIL2});
intent2.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
intent2.putExtra(Intent.EXTRA_TEXT, "Your text here" );
startActivity(intent2);
//Share Files:
//Image:
boolean isPNG = (path.toLowerCase().endsWith(".png")) ? true : false;
Intent i = new Intent(Intent.ACTION_SEND);
//Set type of file
if(isPNG)
{
i.setType("image/png");//With png image file or set "image/*" type
}
else
{
i.setType("image/jpeg");
}
Uri imgUri = Uri.fromFile(new File(path));//Absolute Path of image
i.putExtra(Intent.EXTRA_STREAM, imgUri);//Uri of image
startActivity(Intent.createChooser(i, "Share via"));
break;
//APK:
File f = new File(path1);
if(f.exists())
{
Intent intent2 = new Intent();
intent2.setAction(Intent.ACTION_SEND);
intent2.setType("application/vnd.android.package-archive");//APk file type
intent2.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f) );
startActivity(Intent.createChooser(intent2, "Share via"));
}
break;
Use below method, just pass the subject and body as arguments of the
method
public static void shareText(String subject,String body) {
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("text/plain");
txtIntent .putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
txtIntent .putExtra(android.content.Intent.EXTRA_TEXT, body);
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
Below is the code that works with both the email or messaging app.
If you share through email then the subject and body both are added.
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareString = Html.fromHtml("Medicine Name:" + medicine_name +
"<p>Store Name:" + “store_name “+ "</p>" +
"<p>Store Address:" + “store_address” + "</p>") .toString();
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Medicine Enquiry");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareString);
if (sharingIntent.resolveActivity(context.getPackageManager()) != null)
context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
else {
Toast.makeText(context, "No app found on your phone which can perform this action", Toast.LENGTH_SHORT).show();
}
By Creating an Intent using ACTION_SEND you will be able to put extra its type is Intent.EXTRA_TEXT, the second argument is the text you want to share.
Then by setting the share type as text/plain, the Intent service will bring you all apps that support sharing text
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
Images or binary data:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
or HTML:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<p>This is the text shared.</p>"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Kotlin
Inside the click listener needed to add this module for sharing text via applications like whatsApp, email like Gmail, Slack..
shareOptionClicked.setOnClickListener{
val shareText = Intent(Intent.ACTION_SEND)
shareText.type = "text/plain"
val dataToShare = "Message from my application"
shareText.putExtra(Intent.EXTRA_SUBJECT, "Subject from my application")
shareText.putExtra(Intent.EXTRA_TEXT, dataToShare)
startActivity(Intent.createChooser(shareText, "Share Via"))
}
This code is for sharing through sms
String smsBody="Sms Body";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", smsBody);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
100 % Working Code For Gmail Share
Intent intent = new Intent (Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"anyMail#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Any subject if you want");
intent.setPackage("com.google.android.gm");
if (intent.resolveActivity(getPackageManager())!=null)
startActivity(intent);
else
Toast.makeText(this,"Gmail App is not installed",Toast.LENGTH_SHORT).show();

How to force Share Intent to open a specific app?

I like share intent, it is perfect to open sharing apps with image and text parameters.
But now i'm researching the way to force share intent to open a specific app from the list, with the paramters given to the share intent.
This is my actual code, it shows the list of sharing apps installed on the phone. Please, can somedone tell me what i should add to the code to force for example official twitter app? and official faccebok app?
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file:///sdcard/test.jpg");
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "body text");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
Thanks
For Facebook
public void shareFacebook() {
String fullUrl = "https://m.facebook.com/sharer.php?u=..";
try {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setClassName("com.facebook.katana",
"com.facebook.katana.ShareLinkActivity");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "your title text");
startActivity(sharingIntent);
} catch (Exception e) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(fullUrl));
startActivity(i);
}
}
For Twitter.
public void shareTwitter() {
String message = "Your message to post";
try {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setClassName("com.twitter.android","com.twitter.android.PostActivity");
sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
startActivity(sharingIntent);
} catch (Exception e) {
Log.e("In Exception", "Comes here");
Intent i = new Intent();
i.putExtra(Intent.EXTRA_TEXT, message);
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://mobile.twitter.com/compose/tweet"));
startActivity(i);
}
}
There is a way more generic to do that, and is not necessary to know the full package name of the application intent.
See this post:
How to customize share intent in Android?
100% Working solution
if you want to share something in any app you want or open a url via every action, just use this method:
private void shareOrViewUrlViaThisApp(String appPackageName, String url) {
boolean found = false;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(appPackageName) ||
info.activityInfo.name.toLowerCase().contains(appPackageName) ) {
intent.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(intent, "Select"));
}
}
and simply call:
shareOrViewUrlViaThisApp(<your package name>,<your url>);
This answer was inspired from this.

Categories

Resources