11

I create a bottom navigation bar. I create a menu.xml under res. I add BottomNavigation to main layout.

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"

        app:menu="@menu/menu"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        />

But I want to add item programatically. How can I do? BottomNavigation guide says that

The bar contents can be populated by specifying a menu resource file. Each menu item title, icon and enabled state will be used for displaying bottom navigation bar items. Menu items can also be used for programmatically selecting which destination is currently active. It can be done using MenuItem#setChecked(true)

I understand that using xml is a populated solution but not only way to create bottom navigation.

I add a new item using below code inside onCreate function:

Menu menu = bottomNavigationView.getMenu();
menu.add(0, id, Menu.NONE, "NEW ITEM");

It doesn't work properly. I can see only one item on navigation bar. The item appears(the others disappear) when I can press the area of the item.

EDIT

I find the problem. If I put more than 3 items on navigation bar, this issue occurs. There is a question about this issue. The solution works!

Community
  • 1
  • 1
  • Possible duplicate of [Android BottomNavigationView items showing without text also layout does not hiding on scroll](https://stackoverflow.com/questions/42246928/android-bottomnavigationview-items-showing-without-text-also-layout-does-not-hid) – MrEngineer13 Jul 26 '18 at 18:24

0 Answers0