Inflate Bottom Navigation View menu programmatically
Asked Answered
A

2

29

Bottom Navigation View has been added to version 25 of the Design Support Library. Tried and it's much easier to use now.

But I am facing problem implementing it as per my app requirements. I want to inflate menu resource dynamically and change menu items/titles of the Bottom Navigation view programmatically.

inflateMenu(int menuResource) — Inflate a menu for the bottom navigation view using a menu resource identifier.

According to docs:

inflateMenu: void inflateMenu (int resId) Inflate a menu resource into this navigation view. Existing items in the menu will not be modified or removed. Parameters resId int: ID of a menu resource to inflate

Trying to use this inflateMenu(int resID) method programmatically with navigation view throws exception "Resource not found"

bottomNavigationView.inflateMenu(R.menu.bottom_navigation_menu);

Is it possible to achieve it without any third party libraries?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:id="@+id/main_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation" />

    <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:itemBackground="@color/theme_action_bar_bg"
        app:itemIconTint="@color/white"
        app:itemTextColor="@color/white" />
</RelativeLayout>

Menu Resource:

 <?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">
    <item
        android:id="@+id/nav_bar_item_dashboard"
        android:enabled="true"
        android:icon="@drawable/ic_nav_bar_dashboard_24px"
        android:title="@string/nav_bar_item_dashboard"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/nav_bar_item_people"
        android:enabled="true"
        android:icon="@drawable/ic_nav_bar_people_24px"
        android:title="@string/nav_bar_item_people"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/nav_bar_item_classroom"
        android:enabled="true"
        android:icon="@drawable/ic_nav_bar_classroom_24px"
        android:title="@string/nav_bar_item_classrooms"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/nav_bar_item_manage"
        android:enabled="true"
        android:icon="@drawable/ic_nav_bar_manage_24px"
        android:title="@string/nav_bar_item_manage"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/nav_bar_item_more"
        android:enabled="true"
        android:icon="@drawable/ic_nav_bar_more_24px"
        android:title="@string/nav_bar_item_more"
        app:showAsAction="ifRoom" />
</menu>

Inflating Programmatically in menuresource for Bottom Navigation view inside Activity:

navBar = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    navBar.inflateMenu(R.menu.bottom_navigation_view);
Abstinence answered 29/3, 2017 at 4:2 Comment(1)
post your code and layoutTuxedo
A
50
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.new_navigation_drawer_items);

from this answer

Assault answered 15/6, 2019 at 20:19 Comment(2)
It works like a charm! No space issue with this. Thank you!Sewer
Thanks alot! Saved me the trouble of using multiple BottomNavigationViews, which would cause other headaches.Anthropomorphic
H
34
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);

//Add MenuItem with icon to Menu
navigation.getMenu().add(Menu.NONE, 1, Menu.NONE, "Home").setIcon(R.drawable.ic_home_black_24dp);

Hope it helps.

Handle answered 4/8, 2017 at 7:44 Comment(2)
We can clear menu before adding new items by using navigation.getMenu().clear();Tulle
If I want to provide a custom view to be the menu item, how can I do that?Eldridgeeldritch

© 2022 - 2024 — McMap. All rights reserved.