Search code examples
androidandroid-actionbarandroid-xmlandroid-iconsandroid-actionmode

Setting a custom share icon on Actionbar ShareActionProvider without ActionBarSherlock


I have the same problem as was described here - Setting a custom share icon on Actionbar ShareActionProvider

But I'am not using ActionBarSherlock
I found that the Sherlock theme uses the "actionModeShareDrawable" and I can also use it like this, if I don't use ActionBarSherlock

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

This works fine on my nexus 5, but failed on many other devices
So my question is, how to change that icon without using ActionBarSherlock


Solution

  • You can subclass ShareActionProvider, overriding only the constructor and createActionView().

    From here, you can get the View from super, casting it to ActivityChooserView so you can call setExpandActivityOverflowButtonDrawable(Drawable) to change the icon.

    package com.yourpackagename.whatever;
    
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.support.v7.internal.widget.ActivityChooserView;
    import android.support.v7.widget.ShareActionProvider;
    import android.view.View;
    
    import com.yourpackagename.R;
    
    public class CustomShareActionProvider extends ShareActionProvider {
    
        private final Context mContext;
    
        public CustomShareActionProvider(Context context) {
            super(context);
            mContext = context;
        }
    
        @Override
        public View onCreateActionView() {
            ActivityChooserView chooserView =
                (ActivityChooserView) super.onCreateActionView();
    
            // Set your drawable here
            Drawable icon =
                mContext.getResources().getDrawable(R.drawable.ic_action_share);
    
            chooserView.setExpandActivityOverflowButtonDrawable(icon);
    
            return chooserView;
        }
    }