How to Equally space menu items of BottomNavigationView in Android Tab?

Mohammed, Rahil 0 Reputation points
2024-07-11T12:03:56.5266667+00:00

Screenshot 2024-07-11 at 5.25.28 PM.png

https://github.com/dotnet/maui-samples/tree/main/8.0/Fundamentals/Shell
I've set up this MAUI Shell project and removed one item from the BottomNavigationView. Once removed the remaing three items are concentrated in the center. How to Equally space menu items to fill the width of the Tablet Device

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,777 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,369 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Thomas Smith 0 Reputation points
    2024-07-12T09:03:47.3+00:00

    Hello,

    To achieve equally spaced menu items in a .NET MAUI Shell project, you can follow these steps:

    Custom Renderers:

    Create a custom renderer for the Shell tabs. This allows you to control the appearance and behavior of the tab layout.

    You’ll need to create a platform-specific implementation file (e.g., CustomShellRenderer.cs).

    Customize Tab Layout:

    Override the CreateTabLayoutAppearanceTracker method in your custom renderer.

    Implement your own logic to evenly space the tabs within the tab bar.

    Example Code:

    Here’s a simplified example of how you might structure your custom renderer

    // CustomShellRenderer.cs (Android platform-specific)
    using Android.Content;
    using Microsoft.Maui.Controls;
    using Microsoft.Maui.Controls.Compatibility.Platform.Android;
    using YourAppNamespace.Platforms.Android;
    
    public class CustomShellRenderer : ShellRenderer
    {
        public CustomShellRenderer(Context context) : base(context)
        {
        }
    
        protected override IShellTabLayoutAppearanceTracker CreateTabLayoutAppearanceTracker(ShellSection shellSection)
        {
            return new MyTabLayoutAppearanceTracker(this);
        }
    }
    
    

    Implement Appearance Tracker:

    In your custom appearance tracker (MyTabLayoutAppearanceTracker), adjust the tab widths as needed.

    You can calculate the width based on the number of tabs and the available screen width.Remember to adapt the code snippets to your specific project and customize the appearance as desired.
    https://stackoverflow.com/questions/76376374/net-maui-align-shell-tabsOfficial Site
    Hope this will help you.
    Best regards,
    Thomas Smith


  2. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,081 Reputation points Microsoft Vendor
    2024-07-16T02:36:28.2066667+00:00

    Hello,

    I find you have opened an issue in the MAUI GitHub:Not able to equally space menu items of BottomNavigationView using Shell in Android Tablets #23606

    Please track on this issue in the MAUI GitHub repo.

    I tried to set the visibility mode to LabelVisibilityLabeled in the shell custom render, this issue still existed.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.