I’ve tried to add badge in iOS. I’m able to add successfully. Can someone please help in android. Please not that i don’t want to use shell navigation.
[ExcludeFromCodeCoverage]
public abstract class AppTabbedPageBase : TabbedPage, IViewModelTypeExposed
{
protected AppTabbedPageBase()
{
//Uncomment this if you need to implement custom navigation bar.
NavigationPage.SetHasNavigationBar(this, false);
NavigationPage.SetHasBackButton(this, true);
}
public abstract Type ViewModelType { get; }
protected override bool OnBackButtonPressed()
{
return true;
}
protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
#if ANDROID
FieldInfo tabbedPageManagerFieldInfo = typeof(TabbedPage).GetField("_tabbedPageManager", BindingFlags.NonPublic | BindingFlags.Instance);
object tabbedPageManager = tabbedPageManagerFieldInfo?.GetValue(this);
FieldInfo tabLayoutFieldInfo = tabbedPageManager?.GetType().GetField("_tabLayout", BindingFlags.NonPublic | BindingFlags.Instance);
var tablayout = tabLayoutFieldInfo?.GetValue(tabbedPageManager) as Google.Android.Material.Tabs.TabLayout;
if (tablayout != null)
{
for (int i = 0; i < tablayout.TabCount; i++)
{
tablayout.GetTabAt(i).OrCreateBadge.Number = 10;
// tablayout.GetTabAt(i).OrCreateBadge.BackgroundColor = Resource.Color.blue;
// tablayout.GetTabAt(i).OrCreateBadge.BadgeTextColor = Resource.Color.white;
}
}
#endif
}
}
============
<views:MainTabbedPageXaml
x:Class=”Demo.UI.MainTabbedPage”>
<NavigationPage IconImageSource="navhome.png" Title="Home">
<x:Arguments>
<views:HomePage BindingContext="{Binding HomeViewModel}" />
</x:Arguments>
</NavigationPage>
<NavigationPage IconImageSource="navcart.png" Title="Cart">
<x:Arguments>
<views:CartPage BindingContext="{Binding CartViewModel}" />
</x:Arguments>
</NavigationPage>
<NavigationPage IconImageSource="navmap.png" Title="Maps">
<x:Arguments>
<views:MapsPage BindingContext="{Binding MapsViewModel}" />
</x:Arguments>
</NavigationPage>
<NavigationPage IconImageSource="navprofile.png" Title="Profile">
<x:Arguments>
<views:ProfilePage BindingContext="{Binding ProfileViewModel}" />
</x:Arguments>
</NavigationPage>
</views:MainTabbedPageXaml>
Not sure, how can i access tablayout and add badge over there.
You can modify it by using platform native code.
From the source code of TabbedPage.Android.cs, we can first get the instance of TabbedPageManager
,and from the source code of TabbedPageManager, we can use Reflection
to get TabbedPageManager
(because it is a internal class) and TabLayout
.
Then we can override method OnHandlerChanged()
on the TabbedPage
.
Please refer to the following code:
#if ANDROID
using System.Reflection;
#endif
public partial class HomeTabbedPage : TabbedPage
{
public HomeTabbedPage()
{
InitializeComponent();
}
protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
#if ANDROID
FieldInfo tabbedPageManagerFieldInfo = typeof(TabbedPage).GetField("_tabbedPageManager", BindingFlags.NonPublic | BindingFlags.Instance);
object tabbedPageManager = tabbedPageManagerFieldInfo?.GetValue(this);
FieldInfo tabLayoutFieldInfo = tabbedPageManager?.GetType().GetField("_tabLayout", BindingFlags.NonPublic | BindingFlags.Instance);
var tablayout = tabLayoutFieldInfo?.GetValue(tabbedPageManager) as Google.Android.Material.Tabs.TabLayout;
if (tablayout != null)
{
for (int i = 0; i < tablayout.TabCount; i++)
{
tablayout.GetTabAt(i).OrCreateBadge.Number = 10;
// tablayout.GetTabAt(i).OrCreateBadge.BackgroundColor = Resource.Color.blue;
// tablayout.GetTabAt(i).OrCreateBadge.BadgeTextColor = Resource.Color.white;
}
}
#endif
}
}
And the following screenshot is the result on my side:
14