How to dynamically hide a menu item in BottomNavigationView?

帅比萌擦擦* 提交于 2019-11-30 17:05:24
mBottomNavigationView.getMenu().removeItem(R.id.item_name);

removeItem does the trick. Not sure why setVisible method is not working.

I tried most of solutions but this worked for me,

For hiding an item dynamically : bottomNavigationView.findViewById(R.id.xyz).setVisibility(View.GONE);

For making item visible: bottomNavigationView.findViewById(R.id.xyz).setVisibility(View.VISIBLE);

You can hide a menu item by setting isVisible as false with using suggested property isVisible in Kotlin. But this makes your menu item removed from BottomNavigationView on Android 9 as my observation.

bottomNavigation.menu.findItem(R.id.menu_item).isVisible = false

If you use a single color for your bottom navigation view's background you can use similar approach to save the menu items in place. As an example the one in the right edge.

// 0x000000 is black as an example
bottomNavigation.menu.findItem(R.id.menu_item).icon = ColorDrawable(0x000000)
// and disable for the actions
bottomNavigation.menu.findItem(R.id.menu_item).isEnabled = false

The removeItem displaces the menu items in the bar when an item(s) is hidden. I found a slightly better way. Create a group of menu items that you would want to hide your menu xml.

In your bottom_menu.xml

<menu...>
    <group android:id="@+id/hiddenmenu">
        <item.../>
        <item.../>
    </group>
    <item.../>
</menu>

And in your activity.cs

Menu menu = mbottomNavigation.getMenu();  
menu.getMenu.setGroupEnabled(R.id.hiddenmenu, false);

Although, with this setup, when all menu items are visible, the checked change state of the menu items goes out of whack. Also tried programmatically adding menu items to an empty group but the group stopped responding to the GroupDisable...

Sahitya Pasnoor

setVisibility should work for you. FYI, below example is in kotlin.

bottomNavigationView.menu.findItem(R.id.navigation_item_two).isVisible = false

In my case, I wanted to hide the toolbar text and the icons/titles of BottomNavigationView items in the authorization fragment, which handles the initial loading of my application. When it determines that the user is authenticated and fetches their profile from the database, I load the feed fragment, which fetches data from the database and displays it to the user. What I did was add the following method to the activity that creates the layout elements and call it from its fragments, passing in a boolean to determine visibility of the items.

public void setBottomNavigationViewItemsVisibility(boolean value) {
    if (this.bottomNavigationView != null) {
        this.bottomNavigationView.setVisibility(View.VISIBLE);
        Menu menu = this.bottomNavigationView.getMenu();
        if (value) {
            int[] icons = {R.drawable.ic_event_white_24dp, R.drawable.ic_explore,
                    R.drawable.ic_store_white_24dp, R.drawable.ic_notifications_white_24dp};
            int[] titles = {R.string.feed, R.string.explore, R.string.finder, R.string.notifications};
            for (int i = 0; i < menu.size(); i++) {
                menu.getItem(i).setIcon(icons[i]);
                menu.getItem(i).setTitle(titles[i]);
                menu.getItem(i).setEnabled(true);
            }
        } else {
            for (int i = 0; i < menu.size(); i++) {
                menu.getItem(i).setIcon(R.drawable.ic_empty);
                menu.getItem(i).setTitle(R.string.title_empty);
                menu.getItem(i).setEnabled(false);
            }
        }
    }
}

We declare an array of drawable ids and an array of title ids to match what we have declared in the menu XML file. If true, we iterate through the menu items and set their icon, title, and their state to default values. If false, we set the icon to a transparent icon (removing the icon affects its size), set the toolbar title to an empty string, and disable it.

BottomNavigationView Menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_navigation_view_home">

    <item
        android:id="@+id/action_feed"
        android:enabled="true"
        android:icon="@drawable/ic_event_white_24dp"
        android:title="@string/feed"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_explore"
        android:enabled="true"
        android:icon="@drawable/ic_explore"
        android:title="@string/explore"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_finder"
        android:enabled="true"
        android:icon="@drawable/ic_store_white_24dp"
        android:title="@string/finder"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_notifications"
        android:enabled="true"
        android:icon="@drawable/ic_notifications_white_24dp"
        android:title="@string/notifications"
        app:showAsAction="ifRoom" />
</menu>

Empty Icon (ic_empty.xml):

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">
    <path
        android:fillColor="#00FFFFFF"
        android:pathData="M8" />
</vector>

Empty Title (title_empty):

<string name="title_empty" />

It works for me in kotlin like this:

bottomNavigationView.rootView.findViewById<View>(R.id.nav_nearestRestaurant).visibility = View.GONE

But the thing is that if you click in another item, the hidden item appears again, because the bottom bar reload its appearance when show you the active item (so if in your xml menu, its visible, appears visible again (I think, to me happen like that))

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!