Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Filter out Intents you don"t want to show from a IntentChooser dialog. For example your own app, competing apps or just apps you have a share integration by SDK already :) Based on
// Usage:
// blacklist
String[] blacklist = new String[]{"com.any.package", "net.other.package"};
// your share intent
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "some text");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "a subject");
// ... anything else you want to add
// invoke custom chooser
startActivity(generateCustomChooserIntent(intent, blacklist));
// Method:
private Intent generateCustomChooserIntent(Intent prototype, String[] forbiddenChoices) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
Intent chooserIntent;
Intent dummy = new Intent(prototype.getAction());
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(dummy, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo resolveInfo : resInfo) {
if (resolveInfo.activityInfo == null || Arrays.asList(forbiddenChoices).contains(resolveInfo.activityInfo.packageName))
HashMap<String, String> info = new HashMap<String, String>();
info.put("packageName", resolveInfo.activityInfo.packageName);
info.put("simpleName", String.valueOf(resolveInfo.activityInfo.loadLabel(getPackageManager())));
if (!intentMetaInfo.isEmpty()) {
// sorting for nice readability
Collections.sort(intentMetaInfo, new Comparator<HashMap<String, String>>() {
public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
return map.get("simpleName").compareTo(map2.get("simpleName"));
// create the custom intent list
for (HashMap<String, String> metaInfo : intentMetaInfo) {
Intent targetedShareIntent = (Intent) prototype.clone();
targetedShareIntent.setClassName(metaInfo.get("packageName"), metaInfo.get("className"));
chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), getString(R.string.share));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
return chooserIntent;
return Intent.createChooser(prototype, getString(R.string.share));
Copy link

ghost commented Aug 2, 2013

What licence is applied to this gist?

Copy link

xmenxwk commented Aug 5, 2013

do not use Arrays.asList() inside loop, you are recreating same list again and again.

Copy link

emartynov commented Sep 2, 2013

Doesn't work for Intent.ACTION_VIEW unfortunately

Copy link

raminfoo commented Nov 16, 2013

best ans

Copy link

NoaAAA commented Jan 23, 2014

Wow man , this helped allot!!!!!! thank you thank you!!!!!!!!

Copy link

Nutomic commented Apr 1, 2015

I've created a version of this with a whitelist instead of blacklist, and also cleaned the code in the process:

Copy link

bixir commented Dec 15, 2015

This line:

info.put("simpleName", String.valueOf(resolveInfo.activityInfo.loadLabel(getPackageManager())));

doesn't always produce the expected label. For example, when using Intent.createChooser() I normally have these options: "ES Save to..." and "Send by LAN". but the above code produces in both cases the label "ES File Explorer". Also, it produces "Picasa Uploader" instead of "Picasa". For Picasa, even the icon is wrong (the generic android icon). What can be done to produce the normal labels and icons in all cases?

Copy link

Kevinrob commented Feb 5, 2018

Copy link

ranjeetscience commented Jun 17, 2019

Can anybody explain to me what is significance of this "targetedShareIntents.remove(targetedShareIntents.size() - 1)"

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