Setting a custom share icon on Actionbar ShareActionProvider without ActionBarSherlock
Asked Answered
S

4

6

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

Stultify answered 27/1, 2014 at 9:58 Comment(1)
Could you solve this? I am having the same issue.Allegro
J
8

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;
    }
}
Jackinthebox answered 21/4, 2014 at 18:39 Comment(0)
H
2

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

Hangover answered 22/4, 2014 at 16:32 Comment(0)
L
0

You can use in the menu like this

menu.xml

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

And into your set icon if api>21 android:actionModeShareDrawable otherwise actionModeShareDrawable

Style.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ....
    <item name="actionModeShareDrawable">@drawable/share</item>
</style>

In the last apply this theme

manifest.xml

 <application
     ...
     android:theme="@style/AppTheme" >
Lionfish answered 23/1, 2016 at 9:50 Comment(0)
Y
-1

I know this is not in a theme but I set my share icon in menu.xml. This may help if you are just trying to have a custom icon.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/share" android:showAsAction="always" android:title="@string/share" android:icon="@drawable/ic_menu_share"/>
</menu>

I am surprised it works at all. Try changing actionModeShareDrawalbe to actionModeShareDrawable.

<style name="Theme.MyApp" parent="android:Theme.Holo">
    <item name="android:actionModeShareDrawable">@drawable/icon</item>
</style>
Yautia answered 11/2, 2014 at 16:47 Comment(2)
sorry, that was just a missclick, of course I have drawable, not drawalbe :DStultify
this doesn't work if you're using a ShareActionProvider in your menuLiminal

© 2022 - 2024 — McMap. All rights reserved.