Hamburger menu icon color

aliZAKRIAaliZAKRIA USMember ✭✭✭
edited September 20 in Xamarin.Forms

Hello i use custom render to change Hamburger menu icon color
like this

 [Obsolete]
    public class CustomNavigationPageRenderer : 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 tintColor = Color.Black;
            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.SetColorFilter(tintColor.ToAndroid());
                    //imageButton.SetImageDrawable(Forms.Context.GetDrawable(Resource.Drawable.hamburger));
                }
            }
        }

but when i navigate to home page from click button like this :

    await Navigation.PushModalAsync(new MasterDetailPage1()
                {
                    Detail = new NavigationPage(new MainPage()) {  }
                });

the icon change agine to old color please help

Tagged:

Answers

  • aliZAKRIAaliZAKRIA USMember ✭✭✭

    any help

  • LandLuLandLu Member, Xamarin Team Xamurai

    Remove the Obsolete attribute and adjust your custom renderer like:

    [assembly: ExportRenderer(typeof(MasterDetailPage), typeof(CustomNavigationPageRenderer))]
    namespace MasterDetailPageDemo.Droid
    {
        public class CustomNavigationPageRenderer : MasterDetailPageRenderer
        {
            MainActivity Activity;
            public CustomNavigationPageRenderer(Context context) : base(context)
            {
                Activity = context as MainActivity;
            }
    
            protected override void OnLayout(bool changed, int l, int t, int r, int b)
            {
                base.OnLayout(changed, l, t, r, b);
                var tintColor = Android.Graphics.Color.Black;
    
                var toolbar = Activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    
                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.SetColorFilter(tintColor);
                        //imageButton.SetImageDrawable(Forms.Context.GetDrawable(Resource.Drawable.hamburger));
                    }
                }
            }
    
        }
    }
    

    You can retrieve the current activity directly in the constructor instead of using the plugin.

  • aliZAKRIAaliZAKRIA USMember ✭✭✭

    any help> @LandLu said:

    Remove the Obsolete attribute and adjust your custom renderer like:

    [assembly: ExportRenderer(typeof(MasterDetailPage), typeof(CustomNavigationPageRenderer))]
    namespace MasterDetailPageDemo.Droid
    {
        public class CustomNavigationPageRenderer : MasterDetailPageRenderer
        {
            MainActivity Activity;
            public CustomNavigationPageRenderer(Context context) : base(context)
            {
                Activity = context as MainActivity;
            }
    
            protected override void OnLayout(bool changed, int l, int t, int r, int b)
            {
                base.OnLayout(changed, l, t, r, b);
                var tintColor = Android.Graphics.Color.Black;
                
                var toolbar = Activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    
                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.SetColorFilter(tintColor);
                        //imageButton.SetImageDrawable(Forms.Context.GetDrawable(Resource.Drawable.hamburger));
                    }
                }
            }
        
        }
    }
    

    You can retrieve the current activity directly in the constructor instead of using the plugin.

    thanks for you response , your solution did not work

  • LandLuLandLu Member, Xamarin Team Xamurai

    Please check my sample:

    The button has been changed to black.

Sign In or Register to comment.