Disable ShareActionProvider in ActionBar


Ask by : S.D. August 20, 2014 19:38

I'm tying to disable ShareActionProvider shown in ActionBar's Action Mode. But

menu.findItem(R.id.myShareItem).setEnabled(false)  

does not disables it. Am I missing something or it just can't be disabled ?

UPDATE:

Testing on Jelly Bean, this standalone Activity class demonstrates the problem:

package com.example;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.MimeTypeMap;
import android.widget.ShareActionProvider;
import android.widget.Toast;

public class MyActivity extends Activity {

    private MenuItem mShareItem;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        ShareActionProvider sap = new ShareActionProvider(this);

        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType(MimeTypeMap.getSingleton().getMimeTypeFromExtension("jpg"));
        sap.setShareIntent(shareIntent);


        mShareItem = menu.add("Share");
        mShareItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        mShareItem.setActionProvider(sap);

        MenuItem toggleItem = menu.add("Toggle");
        toggleItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        //---shouldn't this work ? -----
        mShareItem.setEnabled(!mShareItem.isEnabled());

        Toast.makeText(this,mShareItem + " enabled: " + mShareItem.isEnabled(),Toast.LENGTH_SHORT).show();

        return super.onOptionsItemSelected(item);
    }

}
View original question

Answer by : S.D.November 09, 2012 07:23