Created
June 3, 2024 09:06
-
-
Save FlavioGoncalves-Cayas/3f14dea04fe5e2489220be77738e561e to your computer and use it in GitHub Desktop.
Workaround for IconImageSource not working in .NET MAUI Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.Content; | |
using Android.Content.Res; | |
using Android.Graphics.Drawables; | |
using Android.Runtime; | |
using Android.Util; | |
using AndroidX.AppCompat.Graphics.Drawable; | |
using AndroidX.Core.Content.Resources; | |
using Google.Android.Material.AppBar; | |
using JetBrains.Annotations; | |
using Microsoft.Maui.Handlers; | |
namespace Workaround; | |
public class CustomToolbarHandler : ToolbarHandler | |
{ | |
protected override MaterialToolbar CreatePlatformElement() | |
{ | |
var toolbar = base.CreatePlatformElement(); | |
var customMaterialToolbar = new CustomMaterialToolbar(toolbar.Context); | |
customMaterialToolbar.ToolbarHandler = this; | |
customMaterialToolbar.LayoutParameters = toolbar.LayoutParameters; | |
customMaterialToolbar.PopupTheme = toolbar.PopupTheme; | |
return customMaterialToolbar; | |
} | |
class CustomMaterialToolbar : MaterialToolbar | |
{ | |
public ToolbarHandler ToolbarHandler { get; set; } | |
protected CustomMaterialToolbar(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) | |
{ | |
} | |
public CustomMaterialToolbar([NotNull] Context context) : base(context) | |
{ | |
} | |
public CustomMaterialToolbar([NotNull] Context context, IAttributeSet attrs) : base(context, attrs) | |
{ | |
} | |
public CustomMaterialToolbar([NotNull] Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) | |
{ | |
} | |
private Drawable _menuIconDrawable; | |
private bool _isDarkMode; | |
private Drawable GetMenuIconDrawable() | |
{ | |
var isDarkMode = (Context.Resources.Configuration.UiMode & UiMode.NightYes) == UiMode.NightYes; | |
if (_menuIconDrawable == null || _isDarkMode != isDarkMode) | |
{ | |
_isDarkMode = isDarkMode; | |
var resId = isDarkMode ? Resource.Drawable.menuicon_night : Resource.Drawable.menuicon; | |
_menuIconDrawable = ResourcesCompat.GetDrawable(Context.Resources, resId, null); | |
} | |
return _menuIconDrawable; | |
} | |
public override Drawable NavigationIcon | |
{ | |
get => base.NavigationIcon; | |
set | |
{ | |
//Only use our own Drawable when the BackButton is not visible | |
if (ToolbarHandler.VirtualView.BackButtonVisible) | |
{ | |
//Prevent the Animation from showing, as that would show the hamburger for a short amount of time | |
if (value is DrawerArrowDrawable dad) | |
dad.Progress = 1.0f; | |
base.NavigationIcon = value; | |
} | |
else | |
base.NavigationIcon = GetMenuIconDrawable(); | |
} | |
} | |
protected override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) | |
{ | |
base.OnConfigurationChanged(newConfig); | |
//UiMode might have changed: call the setter so the logic will determine which icon to show | |
NavigationIcon = NavigationIcon; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if __ANDROID__ | |
handlers.AddHandler<Toolbar, CustomToolbarHandler>(); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment