androidxamarinshareactionprovider

How to fix the share icon in the support actionbar


I'm trying to implement the share functionality within my app. So far it works fine and I can share text to all other apps. The problem is the way it's shown.

I want something like just the share icon visible and then when user taps on it, it opens the OS dialog and lets user choose the app they want to share content to.

    var share_article = menu.FindItem (Resource.Id.action_share);
    var share_article_provider = (Android.Support.V7.Widget.ShareActionProvider) Android.Support.V4.View.MenuItemCompat.GetActionProvider (share_article);
    share_article_provider.SetShareIntent (CreateIntent ());

and the xml:

<item 
    android:id="@+id/action_share"
    myapp:showAsAction="ifRoom"
    android:title="share"
    myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />

My app currently looks like this:

enter image description here

There's also a white border around it that I don't like. Is there any way to change the icon??

How do I fix it??


Solution

  • As mentioned here when using support library this can be fixed really easily. This method won't turn off the share history but will hide the icons from actionbar. I just needed to subclass the Android.Support.V7.Widget.ShareActionProvider like the following: (C# using Xamarin)

    public class MyShareActionProvider : Android.Support.V7.Widget.ShareActionProvider
    {
        public SingleArticleShareActionProvider (Context context) : base (context)
        {}
    
        public override View OnCreateActionView ()
        {
            return null;
        }
    }
    

    and then inside OnCreateOptionsMenu use the MyShareActionProvider like:

    var share_article = menu.FindItem (Resource.Id.action_share);
    var share = new SingleArticleShareActionProvider (globalContext);
    
    Android.Support.V4.View.MenuItemCompat.SetActionProvider (share_article, share);
    share_article.SetIcon (Resource.Drawable.abc_ic_menu_share_mtrl_alpha);
    share.SetShareIntent (CreateIntent ());
    

    You can use any icon you like with the method SetIcon.