Stark_kids
Stark_kids

Reputation: 547

MenuItem on ActionBar can't be clicked

I'm trying to implement a share intent to share images. I have a full screen activity which extends ActionBarActivity and a fragment that implements the immersivemode as the android developer's guide explains, so the user can see the image in fullscreen. The problem is that even if I can see the share icon on ActionBar, it seems that it can't be clicked, apparently because the method onOptionsItemSelected is never called.

Here's the onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {  
    getMenuInflater().inflate(R.menu.menu_share, menu);
    return true;
}

onOptionsItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.menu_share:
            Intent i = getIntent();
            uriString = i.getStringExtra("uri");
            if(uriString != null) {
                Uri uri = Uri.parse(uriString);

                mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
                mShareActionProvider.setShareIntent(createShareIntent(uri));
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Here's the method I created to prepare and start the intent:

private Intent createShareIntent(Uri uri) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share_image)));
    return shareIntent;
}

menu_share.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">
    <item android:id="@+id/menu_share"
        android:title="Share"
        app:showAsAction="ifRoom"
        android:icon="@drawable/abc_ic_menu_share_mtrl_alpha"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        />
</menu>

I've tried different ways, for instance using setOnMenuItemClickListener or similar things which consider listeners, but I see that these listeners are ignored when I debug. I've also searched for a solution here and on other websites but I can't get how to solve this problem, so any idea will be appreciated. Thanks in advance!

Upvotes: 0

Views: 1238

Answers (2)

Stark_kids
Stark_kids

Reputation: 547

Eventually, I changed my approach to solve the problem and now I'm able to call the onOptionsItemSelected(MenuItem item) method and share images from my app to the others. I put the code below.

onCreateOptionsMenu(Menu menu):

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menuItem = menu.add(Menu.NONE, R.id.action_share, Menu.NONE, R.string.action_share);
    menuItem.setIcon(R.drawable.abc_ic_menu_share_mtrl_alpha);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    mShareActionProvider = new ShareActionProvider(this);
    return true;
}

onOptionsItemSelected(MenuItem item):

@Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.action_share:
                Intent i = getIntent();
                uriString = i.getStringExtra("uri");

                if (uriString != null) {
                    Uri uri = Uri.parse(uriString);

                    MenuItemCompat.setActionProvider(item, mShareActionProvider);
                    createShareIntent(uri);
                    return true;
                }
            default:
                return super.onOptionsItemSelected(item);
        }
    }

My method to create the share intent when I click on the right menu item:

private void createShareIntent(Uri uri) {
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/*");
    if (mShareActionProvider != null) {
        mShareActionProvider.setShareIntent(shareIntent);
    }

}

The id of the item is into ids.xml in values folder:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <item type="id" name="action_share"/>
    </resources>

strings.xml:

<!-- Actions -->
    <string name="action_share">Share image</string>

As you can see, I don't use getMenuInflater().inflate(int menuRes, Menu menu) anymore, but I use the add() method to generate my menu and it works for me.

Upvotes: 0

Paul Woitaschek
Paul Woitaschek

Reputation: 6807

You are inflating the wrong menu. Since your menu file is called menu.xml you should do a

getMenuInflater().inflate(R.menu.menu, menu); 

You dont directly inflate an entry, but the menu.

Also below your

switch (item.getItemId()) {
    case R.id.menu_share:

write: Log.d("mytag", "share menu clicked"); Then you can verify in your android logcat, that there is nothing wrong with the menu clicking, but with the Intent you are receiving from.

Upvotes: 2

Related Questions