Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create "Share" menu in popup menu on Android

I have an android app that has a gridview in it. The gridview items contain among other things, a button to show context sensitive menus. So, I implemented a popup menu that comes up when they touch the button in the gridview item.

This menu contains 3 items:

  1. Edit Item
  2. Delete Item
  3. Share Item

I have successfully implemented the edit and delete menu items. The problem is with the "Share Item" menu item. It is a ShareActionProvider. I previously implemented these menu choices as an ActionMode (menu items across the top). But now that the menu is a popup, I'm not sure how to implement the "Share Item" menu option.

Here is my popup_menu.xml:

     <?xml version="1.0" encoding="utf-8"?>     
      <menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:id="@+id/group_edit_mode">
        <item
            android:id="@+id/MenuItemEdit"
            android:title="@string/item_option_edit"
            app:showAsAction="withText|ifRoom" />

        <item
            android:id="@+id/MenuItemDelete"
            android:title="@string/delete"
            app:showAsAction="withText|ifRoom" />

        <item
            android:id="@+id/MenuItemShare"
            android:title="@string/share"
            app:showAsAction="ifRoom"
            app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
    </group>
</menu>

Here is the popup menu code:

                PopupMenu popupMenu = new PopupMenu(MINMainActivity.getSharedInstance(), optionButton);
            MenuInflater inflater = popupMenu.getMenuInflater();
            inflater.inflate(R.menu.gridview_edit_menu_single_item, popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
            {
                @Override
                public boolean onMenuItemClick(MenuItem item)
                {
                    boolean choiceHandled = false;
                    int itemID = item.getItemId();
                    switch (itemID)
                    {
                        case R.id.MenuItemEdit:
                            MINPageTypeGridFragment.launchAlbumItemDetails(mFragment, albumItem, mPageItem.pageConfigFileName);
                            mFragment.currentMode = MINPageTypeGridFragment.MODE_STANDARD;
                            choiceHandled = true;
                            break;
                        case R.id.MenuItemDelete:
                            MINPageTypeGridFragment.deleteItem(mFragment, mAlbum, albumItem);
                            mFragment.currentMode = MINPageTypeGridFragment.MODE_STANDARD;
                            choiceHandled = true;
                            break;
                        case R.id.MenuItemShare:
                            choiceHandled = true;
                            mFragment.currentMode = MINPageTypeGridFragment.MODE_STANDARD;
                            break;
                    }
                    return choiceHandled;
                }
            });
            popupMenu.show();
like image 524
JustLearningAgain Avatar asked Oct 09 '15 22:10

JustLearningAgain


1 Answers

This was WAY overthought. I just kept it as a button and created a chooser.

public void onShareClick(MINAlbumItem albumItem)
{
    List<MINAlbumItem> albumItemsArray = new ArrayList<MINAlbumItem>();
    albumItemsArray.add(albumItem);

    // Creates intent and loads data from items array
    Intent intent = mFragment.Share(albumItemsArray);
    MINMainActivity.getSharedInstance().startActivity(Intent.createChooser(intent, MINMainActivity.getSharedInstance().getResources().getString(R.string.send_to)));
}
like image 153
JustLearningAgain Avatar answered Nov 15 '22 04:11

JustLearningAgain