1

So I'm trying to add some content to my navigation drawer dynamically. In principle, it works. I can add stuff in my app, and I can make it show up in the navigation drawer. Only problem is, I have to restart the app for the new stuff in the navigation drawer to popup (because I can only get the code working in the onCreate method), which is not the desired behaviour.

This is what I've got in the onCreate method at the moment:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();
MenuItem it1 = menu.add(R.id.group1, 1, 2, "TEST DYNAMIC");

Very basic, but it does what I want. It adds a "test" header to the menu.
What I want, is to be able to access this bit of code at will, so for example when I press a button. I haven't been able to do this myself unfortunately. It doesn't matter if it's in a method in the MainActivity, or in one of my other fragments, but it shouldn't only be run when the MainActivity is created, that's not enough. Also, I probably need a Navigation Drawer re-draw after adding something to it (I'm guessing), but I don't know how to do that either, so any help on that would be great.

Timmiej93
  • 1,328
  • 1
  • 16
  • 35
  • see this answer http://stackoverflow.com/questions/33284812/android-change-navigation-drawer-menu-items-text-programatically/34283820#34283820 – ELITE Feb 24 '16 at 13:46
  • Well, that's pretty much the same as what I've done. The thing I want, and what is not discussed in the answer you linked, is how to access these things outside the onCreate method, because I want to add menu items dynamically, while the app is running, not just when the app is started. – Timmiej93 Feb 24 '16 at 13:48
  • http://stackoverflow.com/questions/30695038/how-to-programmatically-add-a-submenu-item-to-the-new-material-design-android-su – ELITE Feb 24 '16 at 14:05
  • Still nothing about how to use these functions outside the onCreate method though. I do appreciate the effort though. – Timmiej93 Feb 24 '16 at 14:09

1 Answers1

1

I tried the following code

Here button is the variable if type Button and i added onClickListener on it.

EDIT :: In Activity do like this

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        Menu menu = navigationView.getMenu();
        SubMenu topChannelMenu = menu.addSubMenu("Category");
        topChannelMenu.add("Menu Name");
        MenuItem mi = menu.getItem(menu.size()-1);
        mi.setTitle(mi.getTitle());
    }
});

EDIT :: In Fragment do like this

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        Menu menu = navigationView.getMenu();
        SubMenu topChannelMenu = menu.addSubMenu("Category");
        topChannelMenu.add("Menu Name");
        MenuItem mi = menu.getItem(menu.size()-1);
        mi.setTitle(mi.getTitle());
    }
});

Its working...

This adds menu to the end of previous menu.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • Could you expand this to a self contained method? This way I can only use it on my main activity, and not on the fragments I use. – Timmiej93 Feb 24 '16 at 14:35
  • 1
    in fragment try `getActivity().findViewById(R.id.nav_view);` instead of `findViewById(R.id.nav_view);` – ELITE Feb 24 '16 at 14:39
  • That indeed works fine, thank you. Could you incorporate your previous comment to your answer so I can accept it? – Timmiej93 Feb 24 '16 at 14:43
  • 1
    edited the answer..added how to add for both `Activity` and `Fragment`. – ELITE Feb 24 '16 at 14:48