Search code examples
androidcustom-adapternavigation-drawer

Custom menu adapter for navDrawer?


I'm developing an app using the official android NavigationDrawer, the navDrawer has its own menu xml called activity_main_drawer.xml in the picture below:

activity_main_drawer screenshot

This is it in the app:

app screenshot

Now i want to change the titles and icons for each row dynamically (JSON), I know how to make a custom ListView Adapter but i couldn't know how to make a custom menu adapter for this navDrawer. It's not a ListView, it's a menu and has groups and items.

Any help is very appreciated.


Solution

  • You need to add menu items dynamically like below:

    Menu menu = nvDrawer.getMenu();
            for (MenuItem mi : menu.values()) {
                if (menu.size() == 0) {
                   menu.add(mi.getId() + "");
                }
                if (menu.getItem(mi.getId()) == null) {
                    menu.add(mi.getId() + "");
                }
                MenuItem mi = menu.getItem(mi.getId());
                mi.setIcon(mi.getIcon());
                mi.setTitle(mi.getTittle());
            }