Setting a custom share icon on Actionbar ShareActionProvider
Asked Answered
A

5

15

I'm trying to set the icon ShareActionProvider, need a solid white one instead of the semi transparent white one.

However setting in share_menu.xml and code doesn't work. Just wondering whether anyone has got workaround for this before extending the ShareActionProvider. (I'm using actionbarsherlock)

@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
    inflater.inflate(R.menu.share_menu, menu);
    MenuItem actionItem = menu.findItem(R.id.share_action_provider);

    ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
     actionProvider.setShareIntent(mShareIntent);

    // re-setting the icon as it's not being picked up from the menu xml file
    actionItem.setIcon(R.drawable.ic_action_share);

    super.onCreateOptionsMenu(menu, inflater);

}

share_menu.xml

 <item android:id="@+id/share_action_provider"
        android:showAsAction="ifRoom"
        android:title="@string/share_with"
        android:icon="@drawable/ic_action_share"
        android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider" />

Update: Also tried setting it in the style, but no joy

<style name="Theme.MyApp" parent="Theme.Sherlock.Light">
        <item name="actionModeStyle">@style/ActionModeStyle</item>
        <item name="android:actionModeStyle">@style/ActionModeStyle</item>
</style>

<style name="ActionModeStyle" parent="Widget.Sherlock.ActionMode">
      <item name="actionModeShareDrawable">@drawable/ic_action_share</item>
  </style>
Algicide answered 31/5, 2012 at 10:48 Comment(0)
O
17

When I am looking to customize the action bar, usually the first place I look is the ActionBarSherlock themes and styles.

I found that the Sherlock theme uses the "actionModeShareDrawable" as found in theme.xml file.

Try changing your theme to include the "actionModeShareDrawable" item directly.

<style name="Theme.MyApp" parent="Theme.Sherlock.Light">
    <item name="actionModeShareDrawable">@drawable/ic_action_share</item>
</style>
Optimism answered 2/6, 2012 at 17:52 Comment(4)
This will work only because the internal version is used on all platforms. The built-in ShareActionProvider uses a non-public resource to specify the drawable so this solution is very ActionBarSherlock-specific.Aspinwall
Adding actionModeShareDrawable to the main theme, seems to work on both gingerbread and ics.Algicide
Didn't make a difference for me on JB or GB.Duple
I am using ActionBarSherlock 4.4.0 and i'm not getting this piece of code working... @JakeWharton do you have a solution for this? I need to customize this icon.Ochlocracy
V
7

For AppCompat the changes to 2 styles are needed:

1)

<style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarWidgetTheme">@style/Theme.AppCompat.CustomShareIcon</item>
    <item name="android:actionBarWidgetTheme">@style/Theme.AppCompat.CustomShareIcon</item>
</style>

In other words, specifiing attr:actionModeShareDrawable in your MyAppTheme is not what you really need, but it should be mentioned in your attr:actionBarWidgetTheme as following:

2)

<style name="Theme.AppCompat.CustomShareIcon" parent="@style/Theme.AppCompat">
    <item name="actionModeShareDrawable">@drawable/abc_ic_menu_share_holo_dark</item> <!-- your icon goes here -->
</style>
Virginity answered 20/2, 2014 at 23:50 Comment(0)
S
6

UPDATE: Forget about the TRICKY way below, just sub-class ShareActionProvider, return null in onCreateActionView Method, and provide your own icon in menu.xml file, everything will be fine

===========================

I found a very tricky but none-ActionBarSherlock-specific way:

  1. Sub-class ShareActionProvider, with just a little tweak:

    @Override
    public View onCreateActionView() {
        View target = super.onCreateActionView();
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.actionbutton_share, null);
        ImageView overlay = (ImageView) viewGroup.findViewById(R.id.overlay);
        ViewGroup container = (ViewGroup) viewGroup.findViewById(R.id.container);
        container.addView(target);
        container.getLayoutParams().width = overlay.getLayoutParams().width;
        container.getLayoutParams().height = overlay.getLayoutParams().height;
        return viewGroup;
    }
    
  2. Create a layout file (R.layout.actionbutton_share), which place the original view at top but transparent to user (Note the "android:alpha=0" part) :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:alpha="0" />
    
        <ImageView
            android:id="@+id/overlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="7dp"
            android:src="@drawable/yours_whatever_share_icon"
            tools:ignore="ContentDescription" />
    
    </RelativeLayout>
    
  3. Use the hacked ShareActionProvider in your menu.xml file

Scruggs answered 7/3, 2014 at 10:27 Comment(2)
Super . The override and return Null worked beautifullyBehm
The inherit, return null idea should be made its own answer. It worked for me. The way this answer is written is confusing at first.Greatcoat
L
3

vivimice's solution is good, but the icon of last used app for sharing becomes hidden too.

I did custom share icon as a common menu item without any ShareActionProvider, just in onOptionsItemSelected() method used this code:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

It creates not a popup menu, but a chooser dialog.

Lucretialucretius answered 14/4, 2014 at 14:28 Comment(0)
H
1

You can override the non-public attribute like this.

<style name="Theme.MyApp" parent="android:Theme.Holo">
        <item name="*android:actionModeShareDrawalbe">@drawable/icon</item>
</style>

(*) means reference to the non-public resources in Android

Hickory answered 18/11, 2012 at 18:8 Comment(2)
This option causes crashes on various devices. It is highly discouraged to use non-public Android attributes.Hobbyhorse
So is there a solution for non Sherlock themes? I am using custom theme that extends Holo.Light. Thing is, my share action icon needs to be white instead of dark gray icon of Holo.Light. I have tried overriding non-public attribute as shown, but my device crash on start .no need to test further :). So ..any other solution?Sverdlovsk

© 2022 - 2024 — McMap. All rights reserved.