How to change MasterDetailPage menu and back button on Xamarin.Forms Android

I am trying to change the menu icon and back icon for the MasterDetailPage in Xamarin.Forms. I did it with a custom renderer for iOS, but I can't figure out how to do it properly on Android. I found some code and managed to change the icon for the menu, but the back icon doesn't show up now and this probably isn't the correct way to handle it.

Here is what I have:

using System;
using Android.Content;
using Android.Graphics;
using Android.Widget;
using MyApp.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;

[assembly: ExportRenderer(typeof(MyApp.Controls.MasterDetailPage), typeof(MasterDetailRenderer))]
namespace MyApp.Droid.Renderers
{
    public class MasterDetailRenderer : MasterDetailPageRenderer
    {
        public MasterDetailRenderer(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);

            Bitmap srcBitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.menu);
            int srcWidth = srcBitmap.Width;
            int srcHeight = srcBitmap.Height;
            int dstWidth = (int)(srcWidth * 0.6f);
            int dstHeight = (int)(srcHeight * 0.6f);
            Bitmap dstBitmap = Bitmap.CreateScaledBitmap(srcBitmap, dstWidth, dstHeight, true);

            SetNavigationIcon(Forms.Context, dstBitmap);
        }

        private void SetNavigationIcon(Context context, Bitmap bitmap)
        {
            var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            var navIcon = toolbar.NavigationIcon.Callback as ImageButton;

            navIcon?.SetImageBitmap(bitmap);
        }
    }
}

Answers

Sign In or Register to comment.