Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 343
Martin Avatar asked Jan 27 '14 09:01

Martin


2 Answers

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;
    }
}
like image 75
PrplRugby Avatar answered Oct 13 '22 20:10

PrplRugby


I like PrplRugby's answer, but you have to include the ActivityChooserView in your app. I refactored to use reflection which you can find here: https://gist.github.com/briangriffey/11185716

like image 26
Brian Griffey Avatar answered Oct 13 '22 21:10

Brian Griffey