Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Get Menu from Res/menu Programmatically

Tags:

java

android

xml

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;

like image 521
Shubham Gupta Avatar asked May 01 '26 21:05

Shubham Gupta


1 Answers

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);
    }
}
like image 81
Shubham Gupta Avatar answered May 04 '26 11:05

Shubham Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!