Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (C) 2011 The Android Open Source Project
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
 package com.actionbarsherlock.widget;
This is a provider for a share action. It is responsible for creating views that enable data sharing and also to show a sub menu with sharing activities if the hosting item is placed on the overflow menu.

Here is how to use the action provider with custom backing file in a com.actionbarsherlock.view.MenuItem:

  // In Activity#onCreateOptionsMenu
  public boolean onCreateOptionsMenu(Menu menu) {
      // Get the menu item.
      MenuItem menuItem = menu.findItem(;
      // Get the provider and hold onto it to set/change the share intent.
      mShareActionProvider = (ShareActionProvider) menuItem.getActionProvider();
      // Set history different from the default before getting the action
      // view since a call to MenuItem.getActionView() calls
      // com.actionbarsherlock.view.ActionProvider.onCreateActionView() which uses the backing file name. Omit this
      // line if using the default share history file is desired.
      . . .

  // Somewhere in the application.
  public void doShare(Intent shareIntent) {
      // When you want to share set the share intent.

Note: While the sample snippet demonstrates how to use this provider in the context of a menu item, the use of the provider is not limited to menu items.

 public class ShareActionProvider extends ActionProvider {

Listener for the event of selecting a share target.
     public interface OnShareTargetSelectedListener {

Called when a share target has been selected. The client can decide whether to handle the intent or rely on the default behavior which is launching it.

Note: Modifying the intent is not permitted and any changes to the latter will be ignored.

source The source of the notification.
intent The intent for launching the chosen share target.
Whether the client has handled the intent.
         public boolean onShareTargetSelected(ShareActionProvider sourceIntent intent);

The default for the maximal number of activities shown in the sub-menu.
    private static final int DEFAULT_INITIAL_ACTIVITY_COUNT = 4;

The the maximum number activities shown in the sub-menu.
Listener for handling menu item clicks.
        new ShareMenuItemOnMenuItemClickListener();

The default name for storing share history.
    public static final String DEFAULT_SHARE_HISTORY_FILE_NAME = "share_history.xml";

Context for accessing resources.
    private final Context mContext;

The name of the file with share history data.
Creates a new instance.

context Context for accessing resources.
    public ShareActionProvider(Context context) {
         = context;

Sets a listener to be notified when a share target has been selected. The listener can optionally decide to handle the selection and not rely on the default behavior which is to launch the activity.

Note: If you choose the backing share history file you will still be notified in this callback.

listener The listener.
         = listener;

    public View onCreateActionView() {
        // Create the view and set its data model.
        ActivityChooserModel dataModel = ActivityChooserModel.get();
        ActivityChooserView activityChooserView = new ActivityChooserView();
        // Lookup and set the expand action icon.
        TypedValue outTypedValue = new TypedValue();
        Drawable drawable = .getResources().getDrawable(outTypedValue.resourceId);
        // Set content description.
        return activityChooserView;

    public boolean hasSubMenu() {
        return true;

    public void onPrepareSubMenu(SubMenu subMenu) {
        // Clear since the order of items may change.
        ActivityChooserModel dataModel = ActivityChooserModel.get();
        PackageManager packageManager = .getPackageManager();
        final int expandedActivityCount = dataModel.getActivityCount();
        final int collapsedActivityCount = Math.min(expandedActivityCount);
        // Populate the sub-menu with a sub set of the activities.
        for (int i = 0; i < collapsedActivityCounti++) {
            ResolveInfo activity = dataModel.getActivity(i);
            subMenu.add(0, iiactivity.loadLabel(packageManager))
        if (collapsedActivityCount < expandedActivityCount) {
            // Add a sub-menu for showing all activities as a list item.
            SubMenu expandedSubMenu = subMenu.addSubMenu(.collapsedActivityCount,
            for (int i = 0; i < expandedActivityCounti++) {
                ResolveInfo activity = dataModel.getActivity(i);
                expandedSubMenu.add(0, iiactivity.loadLabel(packageManager))

Sets the file name of a file for persisting the share history which history will be used for ordering share targets. This file will be used for all view created by onCreateActionView(). Defaults to DEFAULT_SHARE_HISTORY_FILE_NAME. Set to null if share history should not be persisted between sessions.

Note: The history file name can be set any time, however only the action views created by onCreateActionView() after setting the file name will be backed by the provided file.

shareHistoryFile The share history file name.
    public void setShareHistoryFileName(String shareHistoryFile) {
         = shareHistoryFile;

Sets an intent with information about the share action. Here is a sample for constructing a share intent:

  Intent shareIntent = new Intent(Intent.ACTION_SEND);
  Uri uri = Uri.fromFile(new File(getFilesDir(), "foo.jpg"));
  shareIntent.putExtra(Intent.EXTRA_STREAM, uri.toString());

    public void setShareIntent(Intent shareIntent) {
        ActivityChooserModel dataModel = ActivityChooserModel.get(,

Reusable listener for handling share item clicks.
        public boolean onMenuItemClick(MenuItem item) {
            ActivityChooserModel dataModel = ActivityChooserModel.get(,
            final int itemId = item.getItemId();
            Intent launchIntent = dataModel.chooseActivity(itemId);
            if (launchIntent != null) {
            return true;

Set the activity chooser policy of the model backed by the current share history file if needed which is if there is a registered callback.
    private void setActivityChooserPolicyIfNeeded() {
        if ( == null) {
        if ( == null) {
        ActivityChooserModel dataModel = ActivityChooserModel.get();

Policy that delegates to the ShareActionProvider.OnShareTargetSelectedListener, if such.
    private class ShareAcitivityChooserModelPolicy implements OnChooseActivityListener {
        public boolean onChooseActivity(ActivityChooserModel hostIntent intent) {
            if ( != null) {
                return .onShareTargetSelected(
            return false;
New to GrepCode? Check out our FAQ X