How to Programmatically Add a Submenu Item to the New Material Design Android Support Library

How to programmatically add a submenu item to the new material design android support library

[Update 20-03-2016]
Bug is resolved. So no need to worry.

[This below contetnt is outdated.]

Adding a dynamic menu to NavigationView is currently bug on Design Support library. And I have report it to android bug source tracking. So wait till the bug will fixed. But if you want the temporary solution you can do it. First add your dynamic menu ..

    navView = (NavigationView) findViewById(R.id.navView);
Menu m = navView.getMenu();
SubMenu topChannelMenu = m.addSubMenu("Top Channels");
topChannelMenu.add("Foo");
topChannelMenu.add("Bar");
topChannelMenu.add("Baz");

After adding your menu just write below code ..

    MenuItem mi = m.getItem(m.size()-1);
mi.setTitle(mi.getTitle());

It's currently hacky solution. But work for me ...

[update 26-06-2015]

As I have reported this bug at Android Bug source Now Bug is marked as a future release, here is the link https://code.google.com/p/android/issues/detail?id=176300

So we can say that the bug is no more exist on future library. So you don't have to use tricky solution. i will also update this answer again when Future version release number is maintion for this bug.

How to programmaticallly remove submenu from navigation drawer in Android?

Try

authSubMenu.clear();

before your first

authSubMenu.add();

I just used SubMenu.clear() in an Android app where I was using a third-party library, and I needed to clear out an unwanted submenu from the action bar. (I actually wanted to remove the submenu completely, and this was the only way I could find to do it. It seemed to work.)

That's different from your situation, where authSubMenu is a menu you just added via menu.addSubMenu("Auth"), so you would expect it to be empty. But, as you've seen, it apparently isn't empty: rather, addSubMenu("Auth") returns the existing submenu of that title. (I can't find documentation to that effect; I'm just going by the results you've reported.)

If that really is the case, as it appears to be, then authSubMenu.clear() will remove all existing items from the submenu.

Xamarin.Android : Add the dynamic menu items to Navigation view of DrawerLayout

Answering to my question :

Suppose you have a list of Menu items such as :

List<MenuItems> lst = new List<MenuItems>();
lst = GetMenuItems();

Then in xamarin we can dynamically populate the Menu items with their title and icon as :

NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
foreach (MenuItems objMenuItems in lst)
{
int iconID = (int)typeof(Resource.Drawable).GetField(objMenuItems.IconName).GetValue(null);

navigationView.Menu.Add(objMenuItems.Title).SetIcon(iconID);
}

By this way you can dynamically add the menu items in NavigationView of DrawerLayout.

Dynamic Menus in NavigationView

Seems like kmi.getId() returns int(or long).

But Menu.add(int) adds menu with title from the given string resources, which is usually represented as R.string.something, and not for usual integer values.

Menu.add(CharSequence) does add menu with title of CharSequence, so you need to do some int-to-string conversion like menu.add(kmi.getId() + "");

Runtime navigation drawer items

Here's the code to add an item.

mNavigationView = (NavigationView) context.findViewById(R.id.navigationView);
Menu menu = mNavigationView.getMenu();
SubMenu devicesMenu = menu.addSubMenu(Menu.NONE, DEVICES_MENU_ID, Menu.NONE, "Devices");
//You get your deviceId (nexus5Id, nexus6Id and so on) from your API call
//You should see deviceId in your code, from a loop or network callback
//in place of my hardcoded devices ids
devicesMenu.add(DEVICES_MENU_ID, nexus5Id, "Nexus 5");
devicesMenu.add(DEVICES_MENU_ID, nexus6Id, "Nexus 6");
devicesMenu.add(DEVICES_MENU_ID, nexus5XId, "Nexus 5X");
devicesMenu.add(DEVICES_MENU_ID, nexus6PId, "Nexus 6P");

To get the menu item, to set it checked, change it's icon or anything:

MenuItem device = devicesMenu.find(deviceId);
devicesMenu.setChecked(true);

To remove an item:

devicesMenu.remove(deviceId);

Note that SubMenu extends Menu. I suggest you to check the documentation of Menu and MenuItem.

EDIT

As your ids aren't suitable for int format required by MenuItems, I think you should add a ScrollView/NestedScrollView in place of your NavigationMenu, add classic TextView with drawableLeft for non group items, and exandable View (LinearLayout for example) with group TextView, which expand when clicked, showing a RecyclerView containing all your devices.

This way, you can use a custom adapter and manage your ids the way you want (the best practice however is to use long ids in RecyclerView (and legacy ListView)).

However, I'm not sure that adding all the devices in the NavigationDrawer (be it standard API with menus, or with a RecyclerView) is a good practice, as the devices could be very long, right? For a large devices collection, I would use a standard non grouped "Devices" item in my NavigationDrawer, and then, showing the user either a searchbox, or a list with devices.

How to add a collapsible menu item inside navigation drawer in android?

You can use expandable list views inside navigation drawers, I don't understand why you don't want to use them. More on that can be found here http://developer.android.com/reference/android/widget/ExpandableListView.html

If you insist on not using expandableListView, then the alternative is to design the expansion yourself using the menu's OnSelect for that specific item. Though I really don't know why you want this, you'd just be re-implementing the wheel.

How to create a simple divider in the new NavigationView?

All you need to do is define a group with an unique ID, I have checked the implementation if group has different id's it will create a divider.

Example menu, creating the separator:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

<group android:id="@+id/grp1" android:checkableBehavior="single" >
<item
android:id="@+id/navigation_item_1"
android:checked="true"
android:icon="@drawable/ic_home"
android:title="@string/navigation_item_1" />
</group>

<group android:id="@+id/grp2" android:checkableBehavior="single" >
<item
android:id="@+id/navigation_item_2"
android:icon="@drawable/ic_home"
android:title="@string/navigation_item_2" />
</group>
</menu>


Related Topics



Leave a reply



Submit