Share via

How to change the color of menu icon(hambruger) in MasterDetailsPage Xamarin.Forms ?

Prasanth Kumar 21 Reputation points
2021-04-07T05:48:26.017+00:00

By default, xamarin.forms applies blue color to the navigation bar of the Master Menu pages. Here i have changed the blue color to white. Please find the attached screenshot below.

85095-hamburger-menu.png

Due to the change of color in navigation bar,the menu icon(hamburger) which was already in white color disappeared.
So now,Can someone tell me how to change the color of that menu icon(hamburger)?

Developer technologies | .NET | Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,721 Reputation points Microsoft External Staff
    2021-04-07T09:05:23.823+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    A simple method is to change the hambruger icon to your icon as you want.

    And you could use a custom renderer to achieve this.

    In Android project

    [assembly: ExportRenderer(typeof(MasterDetailPage), typeof(MyMasterDetailRenderer))]  
    namespace MasterApp0316.Droid  
    {  
       public  class MyMasterDetailRenderer: MasterDetailPageRenderer  
        {  
      
            public MyMasterDetailRenderer(Context context) : base(context)  
            {  
            }  
      
            protected override void OnLayout(bool changed, int l, int t, int r, int b)  
            {  
                base.OnLayout(changed, l, t, r, b);  
                var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);  
                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;  
      
      
                    bool displayBack = false;  
                    var app = Xamarin.Forms.Application.Current;  
      
                    var detailPage = (app.MainPage as MasterDetailPage).Detail;  
      
                    var navPageLevel = detailPage.Navigation.NavigationStack.Count;  
                    if (navPageLevel > 1)  
                        displayBack = true;  
      
                    if (!displayBack)  
                        ChangeIcon(imageButton, Resource.Drawable.test1);  
                    //if (displayBack)  
                    //    ChangeIcon(imageButton, Resource.Drawable.test2);  
      
                }  
            }  
      
            private void ChangeIcon(Android.Widget.ImageButton imageButton, int id)  
            {  
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)  
                    imageButton.SetImageDrawable(Context.GetDrawable(id));  
                imageButton.SetImageResource(id);  
            }         
        }  
    }  
    

    Refer: https://forums.xamarin.com/discussion/179760/menu-bar-icon-not-showing

    And there is a similar thread about this, you can check it here: https://github.com/xamarin/Xamarin.Forms/issues/12274

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.