Android has new ui element - BottomNavigationView
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?
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With