Ivan Kolev
Ivan Kolev

Reputation: 167

Custom "Share to" view in Android

Hello I am developing an android app and I do not want to use the system "Share to" window, I want to customize it. So is there any way to get a list of possible share intents and put them in a custom ListView Layout ?

Upvotes: 2

Views: 1125

Answers (3)

Rob Meeuwisse
Rob Meeuwisse

Reputation: 2937

Yes, you can.

The first step is to build the sharing intent:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");

And then, instead of creating a chooser intent, you ask the package manager for the activities that can service the sendIntent:

List<ResolveInfo> infos = getPackageManager().queryIntentActivities(sendIntent, 0);

This will give you the list of activities (apps) that the chooser would also show.

You can get the app's icon and label from ResolveInfo and show them in a list to the user:

ResolveInfo info; 
Drawable icon = info.loadIcon(getPackageManager());
String label = info.loadLabel(getPackageManager());

Once the user has selected a ResolveInfo you can enrich your sendIntent with the selected activity to handle your intent and then start the activity:

sendIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
startActivity(sendIntent);

We asked the system which activities can handle sendIntent and then let the user pick one of them. So, by definition, that activity can handle sendIntent. Setting the class name of the activity on the sendIntent sends the intent directly to that activity.

Upvotes: 5

Pawel Urban
Pawel Urban

Reputation: 1326

You can use resolveActivity() method in Intent class. So you have to create an Intent to share some content as you do it for now, but instead calling startActivity() you can retrieve matching Activities by calling resolveActivity().

http://developer.android.com/reference/android/content/Intent.html#resolveActivity%28android.content.pm.PackageManager%29

With Activity informations you can build your custom Share dialog/screen.

EDIT

Sorry for my mistake - resolveActivity returns only one ComponentName - so you retrieve just package name and not a list but just one object. If you want to retrieve complete info about Activities (multiple activities matching your Intent) you should use PackageManager.queryIntentActivities() - http://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities%28android.content.Intent,%20int%29

Upvotes: 1

Related Questions