Android - How to add an item to the bottom navigation bar

I am creating a bottom navigation bar. I am creating a menu.xml file under res. I am adding BottomNavigation to the 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 an item programmatically. How can i do The BottomNavigation guide says that

The contents of the panel can be filled by specifying the menu resource file. Each menu item name, icon, and enabled status will be used to display the bottom positions of the navigation bar. Menu items can also be used to programmatically select which destination is currently active. This can be done using MenuItem # setChecked (true)

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

, onCreate:

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

. . ( ), .

. 3 , . question . !

+6

Source: https://habr.com/ru/post/1017149/


All Articles