Question

For the first time i use the contextual actionbar. When i click in the item of my ListView i want have the possibility to share that item in some way (whatsapp/mail/gmail/drive etc etc..). I'm trying in with this method but no works: the menu layout:

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">

        <item
            android:id="@+id/shareButton"
            android:actionProviderClass="android.widget.ShareActionProvider"
            android:title="@string/share"
            android:showAsAction="always"/>
</menu>

The contextual actionbar class:

public class CABMode implements ActionMode.Callback {
private ShareActionProvider mshare;
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.cab, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // don't need to do anything
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        mshare = (ShareActionProvider) item.getActionProvider();
        mshare.setShareIntent(Share());
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        Mode = null;
    }

 public Intent Share() {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, "Share");
    return intent;
 }

}

Well, the contextual actionbar it opens well when i do an long click, but the share button in the actionbar doesn't work. When i say "not works" i mean that seems not clickable. Nothing works when i click over the item. Nothing appears. What i want it's open the multiple share ways. Thanks

Was it helpful?

Solution

When i say "not works" i mean that seems not clickable

ActivityChooserView, the action view ShareActionProvider returns, disables the "share" button when there are no items to display, which in your case if because you're setting up ShareActionProvider incorrectly. All of the "on click" events are handled internally, you won't receive any callbacks to ActionMode.Callback.onActionItemClicked.

Rather than settings it up in ActionMode.Callback.onActionItemClicked, move that code into ActionMode.Callback.onCreateActionMode and call Menu.findItem to initialize it.

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.getMenuInflater().inflate(R.menu.cab, menu);

        final MenuItem item = menu.findItem(R.id.your_share_action_provider_id);
        mshare = (ShareActionProvider) item.getActionProvider();
        mshare.setShareIntent(Share());
        return true;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top