How to Change ShareActionProvider Icon?


#1

I’m adding a ShareActionProvider: note_menu.xml

        <item
        android:title="@string/share"
        android:id="@+id/share"
        android:icon="@drawable/share"
        app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        app:showAsAction="always" />

onCreateOptionsMenu.xml

       @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.note_menu,menu);
        MenuItem menuItem= menu.findItem(R.id.share);
        ShareActionProvider shareActionProvider= (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
        Intent intent= new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        shareActionProvider.setShareIntent(intent);
        return super.onCreateOptionsMenu(menu);
    }

I have supplied a white share icon using android:icon="drawable/share" in my note_menu.xml but the icon didn’t seems to change and it shows the default black icon when I’m running the app on emulator.

One more question why these code are not working on ShareActionProvider(Both of them returning null o fragment while working on Activity):

ShareActionProvider shareActionProvider= (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

or

ShareActionProvider shareActionProvider= (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

This is working on Fragment(It works but I don’t know why, needs some explanation):

 ShareActionProvider shareActionProvider= new ShareActionProvider(getActivity());
        Intent intent= new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        shareActionProvider.setShareIntent(intent);

#2

I do not think that an <item> with app:actionProviderClass uses the android:icon. I think that the action provider draws its own icon. Your results suggest that you have a theme issue, where ShareActionProvider thinks that you have a light action bar (so it should render itself in a dark color).

why these code are not working on ShareActionProvider

I have no idea, sorry.


#3

@mmurphy
I have solved this issue making a custom ShareActionProvider:

   public class CusShareActionProvider extends ShareActionProvider {

    private final Context mContext;
    public CusShareActionProvider(Context context) {
        super(context);
        this.mContext=context;
    }

    @Override
    public View onCreateActionView() {
        View view = super.onCreateActionView();
        if (view != null)
        {
            try
            {
                Drawable icon = mContext.getResources().getDrawable(R.drawable.share);
                Method method = view.getClass().getMethod("setExpandActivityOverflowButtonDrawable", Drawable.class);
                method.invoke(view, icon);
            }
            catch (Exception e)
            {
                Log.e("MyShareActionProvider", "onCreateActionView", e);
            }
        }

        return view;
    }
}

But I’m facing problem changing the icon of Action Overflow, Is there any way to change the Icon of ACtion Overflow or Overflow Menu?


#4

No, you have implemented an ugly unreliable hack.

But I’m facing problem changing the icon of Action Overflow, Is there any way to change the Icon of ACtion Overflow or Overflow Menu?

Fix your theme to be correct, as I suggested in my earlier answer. If you want light icons, use a .DarkActionBar theme.


#5

@mmurphy
I tried using themes but that only works on API 21 and above


#6

Having a theme that ends in .DarkActionBar is available for API Level 11 and higher. This will give you white text and white icons for your action bar.


#7

So what to do, Is there anything else?


#8

yes this helps me :grinning::grinning::grinning:


#9

@mmurphy

toolbar.setOverflowIcon(getResources().getDrawable(R.drawable.custom_icon));

Using Toolbar instead of Actionbar and calling method .setOverflowicon() does the job and it works fine on all API levels…

:grinning::grinning: