Home » Android » android – How to hide the share action (which use most) icon near the share action provider?

android – How to hide the share action (which use most) icon near the share action provider?

Posted by: admin May 14, 2020 Leave a comment

Questions:

See the picture. How can i hide the icon “P” which means share to Pinterest?

enter image description here

They are both on the action bar and I use ActionBarSherlock.

How to&Answers:

If you wish to keep all the share history data model, but just don’t want the extra “default share activity” icom. The answer at How do you turn off share history when using ShareActionProvider? is not good enough.

What you should do is:

  • Copy these classes from the ActionBarSherlock to your project code
    • ShareActionProvider.java
    • ActivityChooserView.java
  • At your ShareActionProvider.java class, import the ActivityChooserView.java which you just copied instead of the ActionBarShelock file location
  • At the ActivityChooserView.java –
    • find the line if (activityCount > 0 && historySize > 0)
    • replace this line with if (false) (it’s pretty ugly, but it’s the quickest fix. you can delve into the code to remove all occurrences of DefaultActivity implementation)

Edit:
Don’t forget to set the new ActionProvider to your menu item, from XML it would look like: android:actionProviderClass="com.*.CustomShareActionProvider"

That’s it!

Answer:

I found a way to work around this. I am using support library 23.0.1, I have not tested this on other support library versions.

The solution is easy, when you create ShareActionProvider, just override method onCreateActionView() and return null for it. Then you can track all history in the popup menu, but the history will not be shown in toolbar.

Here is a code sample:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.add(Menu.NONE, R.id.menu_share, Menu.NONE, R.string.share);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    mShareActionProvider = new ShareActionProvider(this) {
        @Override
        public View onCreateActionView() {
            return null;
        }
    };
    item.setIcon(R.drawable.abc_ic_menu_share_mtrl_alpha);
    MenuItemCompat.setActionProvider(item, mShareActionProvider);
    return true;
}

Currently I have not found any problem using this work around.

Answer:

Based off Sean’s answer I created the necessary classes, you can copy them into your project (https://gist.github.com/saulpower/10557956). This not only adds the ability to turn off history, but also to filter the apps you would like to share with (if you know the package name).

private final String[] INTENT_FILTER = new String[] {
        "com.twitter.android",
        "com.facebook.katana"
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.journal_entry_menu, menu);

    // Set up ShareActionProvider's default share intent
    MenuItem shareItem = menu.findItem(R.id.action_share);

    if (shareItem instanceof SupportMenuItem) {
        mShareActionProvider = new ShareActionProvider(this);
        mShareActionProvider.setShareIntent(ShareUtils.share(mJournalEntry));
        mShareActionProvider.setIntentFilter(Arrays.asList(INTENT_FILTER));
        mShareActionProvider.setShowHistory(false);
        ((SupportMenuItem) shareItem).setSupportActionProvider(mShareActionProvider);
    }

    return super.onCreateOptionsMenu(menu);
}