OiO.lk Blog Android How can i add badge on tabbar in maui android
Android

How can i add badge on tabbar in maui android


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.

public class CustomTabbedPageHandler : Microsoft.Maui.Controls.Handlers.Compatibility.TabbedRenderer
{
    public void UpdateTabBadges()
    {
        if (Element is MainTabbedPage customTabbedPage)
        {
            // Ensure that ViewController is not null
            if (ViewController is UITabBarController tabBar && tabBar.TabBar != null)
            {
                for (int i = 0; i < customTabbedPage.Children.Count; i++)
                {
                    var badgeCount = customTabbedPage.GetBadgeCount(i);
                    AddBadgeToTab(i, badgeCount);
                }
            }
        }
    }

    public void AddBadgeToTab(int tabIndex, int count)
    {
        if (ViewController is UITabBarController tabBar)
        {
            try
            {
                // Check if the tabBar has items and if the tabIndex is valid
                if (tabBar.TabBar.Items != null && tabBar.TabBar.Items.Length > tabIndex)
                {
                    // Set the badge value
                    tabBar.TabBar.Items[tabIndex].BadgeValue = count > 0 ? count.ToString() : null;
                }
            }
            catch (Exception ex)
            {
                // Optionally log the exception or handle it as needed
                Console.WriteLine($"Error adding badge: {ex.Message}");
            }
        }
    }
}

Here is for android. but getting tablayout always null.

public class CustomTabbedPageHandler : TabbedViewHandler
{
    public override void UpdateValue(string property)
    {
        base.UpdateValue(property);

        var tabs = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity?.FindViewById<FragmentContainerView>(Microsoft.Maui.Resource.Id
            .navigationlayout_bottomtabs);

        var tabLayout = tabs?.GetChildAt(0);

        if (tabLayout is TabLayout layout)
        {
            ====//===
        }
    }


}

Not sure, how can i access tablayout and add badge over there.



You need to sign in to view this answers

Exit mobile version