How to hide the share action (which use most) icon near the share action provider?
Asked Answered
G

3

11

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.

Grefer answered 2/4, 2013 at 3:16 Comment(1)
#10707064Dandify
O
12

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!

Oran answered 25/6, 2013 at 6:8 Comment(4)
Am I missing something, because I can't implement ActivityChooserModelClient now?Feu
Also (my bad for not editing earlier); if you leave the activity with the ActionProvider and go back to it, you'll see an empty [ ] box where an icon would be normally.Feu
I got problems with this solution. Couldn't import com.actionbarsherlock.widget.ActivityChooserModel from my custom classes (blocked to outer packages). Even copying this class to my package, it didn't worked (app crashes) :/Riley
You could also opt to grab the code from google's source using the support library. The same modifications apply. github.com/android/platform_frameworks_support/tree/masterAstaire
D
12

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.

Disloyalty answered 11/11, 2015 at 6:54 Comment(1)
I'm a witness from (almost) Q4 of 2017, this does indeed do away with the history thingy, under support library version 26.0.1.Erythrocytometer
A
4

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);
}
Astaire answered 12/4, 2014 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.