Can you bind the hamburger menu icon in android?

stesvisstesvis USMember ✭✭✭

I would like to do something like:

        <MasterDetailPage.Master>
            <ContentPage Title="Menu" IconImageSource="{Binding MyIcon}" BackgroundColor="#454d58">

But in Android it does not work. Is there a way to achieve that?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Nope. If you want to change the hamburger menu icon in android, you should acheve a custom renderer for your MasterDetailPage

    Here is running screenshot.

    Here is code.

     [assembly: ExportRenderer(typeof(MainPage), typeof(IconNavigationPageRenderer))]
     namespace App61.Droid
     {
       [Obsolete]
      public class IconNavigationPageRenderer : MasterDetailPageRenderer
      {
    
    
          private static Android.Support.V7.Widget.Toolbar GetToolbar() => (CrossCurrentActivity.Current?.Activity as 
           MainActivity)?.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    
          protected override void OnLayout(bool changed, int l, int t, int r, int b)
          {
            base.OnLayout(changed, l, t, r, b);
            var toolbar = GetToolbar();
            if (toolbar != null)
            {
                for (var i = 0; i < toolbar.ChildCount; i++)
                {
                    var imageButton = toolbar.GetChildAt(i) as Android.Widget.ImageButton;
    
                    var drawerArrow = imageButton?.Drawable as DrawerArrowDrawable;
                    if (drawerArrow == null)
                        continue;
    
                    imageButton.SetImageDrawable(Forms.Context.GetDrawable(Resource.Drawable.newIcon));
                }
               }
             }
          }
        }
    

    Here is test Icon

  • stesvisstesvis USMember ✭✭✭
    edited October 22

    Thanks @LeonLu if i am not mistaken that can set a different icon but it's basically hardcoded right?
    How can you change it using Binding? Is it still possible?

    Also, what about the [Obsolete] attribute on IconNavigationPageRenderer?

  • stesvisstesvis USMember ✭✭✭

    @LeonLu the other downside that i am finding is that it shows the custom hamburger menu on all pages, even when it should display the back arrow.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @stesvis said:
    Thanks @LeonLu if i am not mistaken that can set a different icon but it's basically hardcoded right?
    How can you change it using Binding? Is it still possible?

    Also, what about the [Obsolete] attribute on IconNavigationPageRenderer?

    No, ContentPage did not have these properties to binding.

    [Obsolete] means that if we not create a constructor for the IconNavigationPageRenderer , this way is Obsolete, we could create a constructor with context attributes. If we do not want to create it. add the [Obsolete] tag, it will told other class that want to inherit IconNavigationPageRenderer not to do that.

  • stesvisstesvis USMember ✭✭✭
    Either way, this solution causes the back arrow to be replaced by the hamburger, but thanks anyway
  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Yes, it will change all of the buttons.

Sign In or Register to comment.