belongs to Maven artifact com.android.support:appcompat-v7:28.0.0-alpha1
Provides a share action, which is suitable for an activity's app bar. Creates views that enable data sharing. If the provider appears in the overflow menu, it creates a submenu with the appropriate sharing actions.
Adding a share action
To add a "share" action to your activity, put a
ShareActionProvider in the app bar's menu resource. For
<item android:id="@+id/action_share" android:title="@string/share" app:showAsAction="ifRoom" app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
You do not need to specify an icon, since the
ShareActionProvider widget takes care of its own appearance and
behavior. However, you do need to specify a title with
android:title, in case the action ends up in the overflow
Next, set up the intent that contains the content your activity is
able to share. You should create this intent in your handler for
and update it every time the shareable content changes. To set up the
- Get a reference to the ShareActionProvider by calling
getActionProvider()and passing the share action's
MenuItem. For example:
MenuItem shareItem = menu.findItem(R.id.action_share); ShareActionProvider myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
- Create an intent with the
ACTION_SENDaction, and attach the content shared by the activity. For example, the following intent shares an image:
Intent myShareIntent = new Intent(Intent.ACTION_SEND); myShareIntent.setType("image/*"); myShareIntent.putExtra(Intent.EXTRA_STREAM, myImageUri);
setShareIntent()to attach this intent to the action provider:
- When the content changes, modify the intent or create a new one,
setShareIntent()again. For example:
// Image has changed! Update the intent: myShareIntent.putExtra(Intent.EXTRA_STREAM, myNewImageUri); myShareActionProvider.setShareIntent(myShareIntent);
Share target rankings
The share action provider retains a ranking for each share target, based on how often the user chooses each one. The more often a user chooses a target, the higher its rank; the most-commonly used target appears in the app bar as the default target.
By default, the target ranking information is stored in a private
file with the name specified by
DEFAULT_SHARE_HISTORY_FILE_NAME. Ordinarily, the share action provider stores
all the history in this single file. However, using a single set of
rankings may not make sense if the
share action provider is used for different kinds of content. For
example, if the activity sometimes shares images and sometimes shares
contacts, you would want to maintain two different sets of rankings.
Listener for the event of selecting a share target.
The default name for storing share history.
Creates a new instance.
Determines if this ActionProvider has a submenu associated with it.
Factory method for creating new action views.
Called to prepare an associated submenu for the menu item backed by this ActionProvider.
Sets a listener to be notified when a share target has been selected.
Sets the file name of a file for persisting the share history which history will be used for ordering share targets.
Sets an intent with information about the share