Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a menu dynamically to bottom navigation view?

Tags:

Android has new ui element - BottomNavigationView

enter image description here

I don't want to contain my menus in the xml files. I will receive the information about menu items and order from backend side. I want to create them dynamically and set into the BottomNavigationView in the onCreate() method. Can I do this?

like image 358
Kiryl Belasheuski Avatar asked Dec 27 '16 12:12

Kiryl Belasheuski


People also ask

How do I create a bottom navigation menu?

To create a Menu Resource File , click on the app -> res -> menu(right-click) -> New -> Menu Resource File and name it bottom_nav_menu. Now the user can create as many items as he wants in the bottom_nav_menu. xml file. The user also needs to create an icon for each of these items.


1 Answers

By default, BottomNavigationView starts with an empty menu. You can use the getMenu() method to get the Menu instance, then add menu items as in the response above. For example,

BottomNavigationView bottomNavigation = findViewById(R.id.bottom_navigation); Menu menu = bottomNavigation.getMenu(); menu.add(Menu.NONE, MENU_ITEM_ID_ONE, Menu.NONE, getString(R.string.str_menu_one))     .setIcon(R.drawable.ic_action_one); 
like image 153
ehartwell Avatar answered Sep 18 '22 13:09

ehartwell