Skip to content
Advertisement

Android Get Menu from Res/menu Programmatically

I am creating a custom BottomNavigation using LinearLayout. I want to use Menu as items.

<com.sam.grocerystore.views.XBottomNavigation
    android:id="@+id/customBottomNav"
    android:layout_width="match_parent"
    android:layout_height="55dp"/>

In Java

XBottomNavigation xNav = new XBottomNavigation(this);

xNav.inflateMenu(R.menu.bottom_nav_menu);

In XBottomNavigation

public void inflateMenu(@MenuRes int menuRes){
    Menu menu = getMenuFromResID(menuRes);
}

Can someone help me to create getMenuFromResID funtion;

Advertisement

Answer

I solved this problem using PopupMenu

public void inflateMenu(@MenuRes int menuRes) {
    PopupMenu p = new PopupMenu(getContext(), null);
    menu = p.getMenu();
    new MenuInflater(getContext()).inflate(menuRes, menu);
    for (int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        // yourFunctionToCreateItemUI(i, item);
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement