Add items to menu group programatically in navigation view - android

I am trying to add menu items to menu group pragmatically but I found no way to do that. I am using Navigation View and added below mentioned menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/nav_lang_currency"
android:title="" />
<item
android:id="#+id/nav_home"
android:title="" />
<group android:id="#+id/nav_refer" />
<item
android:id="#+id/nav_setting"
android:title="" />
<item
android:id="#+id/nav_about_us"
android:title="" />
<item
android:id="#+id/nav_logout"
android:title="" />
</menu>
Everything looks good as mentioned.
I just want to add multiple menu items in nav_refer group at run-time as per business requirement but I found no way to do that.
I searched solution on SO but found no way to do that.
Kindly suggest me how to add multiple menu items in group at run-time.

To add menu to a particular group, call this method Menu.add(int groupId, int itemId, int order, CharSequence title)
Menu menu = navigationView.getMenu();
menu.add(R.id.nav_refer, 123, Menu.NONE, "Title1");
menu.add(R.id.nav_refer, 124, Menu.NONE, "Title2");
menu.add(R.id.nav_refer, 125, Menu.NONE, "Title3");
Important : Initially if you have empty group then newly added items will appear in bottom, to solve this you need to mention orders for groups. add a attribute for all your groups android:orderInCategory="101"

You can do something like this:
NavigationView navView = (NavigationView) findViewById(R.id.navView);
Menu menu = navView.getMenu();
SubMenu subMenu = menu.addSubMenu("sub menu");
subMenu.add("item 1");
subMenu.add("item 2");
subMenu.add("item 3");

create res --> menu file new layout
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item android:title="حساب کاربری">
<menu>
<group android:title="حساب کاربری">
<item android:title="ورود" />
<item android:title="ثبت نام" />
</group>
</menu>
</item>
</group>
<group android:checkableBehavior="single">
<item android:title="سایت">
<menu>
<group android:title="حساب کاربری">
<item android:title="ورود" />
<item android:title="ثبت نام" />
</group>
</menu>
</item>
</group>
</menu>
set NavigationView to app:menu="#menu/drawer_view"
i hope help you

Related

Android : Navigation Menu Item's Group Sequences

I created a navigation menu XML file for my project.
nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
android:id="#+id/group_note"
android:checkableBehavior="single">
<item
android:id="#+id/note"
android:icon="#drawable/all_notes"
android:iconTint="#color/black1"
android:title="Notes"/>
</group>
<group
android:id="#+id/group_days"
android:checkableBehavior="single">
<!-- all days will come here -->
</group>
<group
android:id="#+id/group_setting"
android:checkableBehavior="none">
<item
android:id="#+id/setting"
android:icon="#drawable/setting"
android:iconTint="#color/black1"
android:title="Settings" >
</item>
</group>
group_days item will dynamically. But the problem is,
I except group_days after group_note. But it comes after group_setting!
I don't know why.
I think problem is that all items that you added in XML have default order and that means next added items will go below already added items like you have in your scenario. Try to give an order to your items for example for Notes it will obviously be 0: android:orderInCategory="0" and for Settings try: android:orderInCategory="2" and then for each new added item set category to 1. For example:
menu.add(R.id.monday, Menu.NONE, 1, "Monday");
menu.add(R.id.tuesday, Menu.NONE, 1, "Tuesday");
//and so on

Add item to navigationView

I have made this .xml that contains items of my navigationView.
<item android:title="Title">
<menu>
<group
android:id="#+id/menu"
android:checkableBehavior="single">
<item
android:id="#+id/a"
android:title="A"
android:orderInCategory="1"
/>
<item
android:id="#+id/b"
android:title="B"
android:orderInCategory="1"
/>
<item
android:id="#+id/c"
android:title="C"
android:orderInCategory="1"
/>
</group>
</menu>
</item>
I want to add items bellow item C on the same group programmatically?
In your code:
Menu menu = navigationView.getMenu();
//params: groupId, itemId, order, title
menu.add(R.id.menu, 123, Menu.NONE, "Item");
to remove the divider add this line to your style xml:
<item name="android:listDivider">#android:color/transparent</item>
Use method
navigationView.getMenu().add(int groupId, int itemId, int order, CharSequence title);
Like
getMenu.add(R.id.submenu_others, R.id.action_logout, Menu.NONE, "logout");

Remove Divider from NavigationView items

I made a navigation view populated by this .xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Menu A"
>
<menu
>
<group
android:checkableBehavior="single"
android:orderInCategory="2"
android:id="#+id/gr"
>
<item
android:id="#+id/a1"
android:title="A1"
/>
</group>
</menu>
</item>
<item android:title="Menu B"
android:orderInCategory="3"
>
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/b1"
android:title="B1"
/>
<item
android:id="#+id/b2"
android:title="B2"
/>
<item
android:id="#+id/b3"
android:title="B3"
/>
</group>
</menu>
</item>
I want to add one item programatically to my NavigationView.
I am adding that item with this line of code:
MenuItem item = menu.add(R.id.gr, Menu.NONE, 2, "A2");
Above that item (A2) appears a delimiter. I don't want that delimiter. How can I remove it?
Note: I don't want to put the color of my delimiters transparent. That will make all delimiters disappear.
Add the new item to the SubMenu, not to the Menu. So the delimiter above the item will not appear.
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.getItem(0);
SubMenu subMenu = menuItem.getSubMenu();
subMenu.add(R.id.gr, Menu.NONE, 2, "A2");
subMenu.setGroupCheckable(R.id.gr, true, true);
Call subMenu.setGroupCheckable(R.id.gr, true, true) if you want to define the checkable behaviour for the group. This should be called after the items of the group have been added.

Menu items get added outside of the group

I have a very nice NavigationView with a menu.
This menu consists of two groups, menu_top and menu_bottom.
Relevant menu XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single"
android:id="#+id/menu_top">
<item
android:id="#+id/nav_frontpage"
android:icon="#android:drawable/ic_menu_view"
android:title="#string/menu_home" />
</group>
<group android:checkableBehavior="single"
android:id="#+id/menu_bottom">
<item
android:id="#+id/nav_login"
android:icon="#android:drawable/ic_secure"
android:title="#string/menu_login"
/>
<item
android:id="#+id/nav_register"
android:icon="#android:drawable/ic_menu_view"
android:title="#string/menu_register" />
</group>
</menu>
I try to add multiple items to the menu_top group, but this would not work.
It instead adds it below the menu_bottom group. You can see this because my logout button is in between two lines, so above this button is a group and below this button is a group.
Relevant Java code:
Menu mainMenu = navigationView.getMenu();
for(PageModel page : pages) {
MenuItem pageButton = mainMenu.add(R.id.menu_top,Menu.NONE, Menu.NONE,page.title.rendered);
}
Screenshot:
Try this: android:orderInCategory="999" for the bottom group.
Code will look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single"
android:id="#+id/menu_top">
<item
android:id="#+id/nav_frontpage"
android:icon="#android:drawable/ic_menu_view"
android:title="#string/menu_home" />
</group>
<group android:checkableBehavior="single"
android:id="#+id/menu_bottom"
android:orderInCategory="999">
<item
android:id="#+id/nav_login"
android:icon="#android:drawable/ic_secure"
android:title="#string/menu_login"
/>
<item
android:id="#+id/nav_register"
android:icon="#android:drawable/ic_menu_view"
android:title="#string/menu_register" />
</group>
</menu>

How to show/hide submenus inside NavigationView android

I want to create an expandablelistview like view inside navigation drawer where initially only main titles are shown (collapsed submenus). When title is clicked the submenus should appear (if possible animate)
I am using android.support.design.widget.NavigationView. I am able to add submenus via xml.
<group android:checkableBehavior="all">
<item
android:id="#+id/action_venues"
android:title="TITLE 1">
<menu>
<group
android:id="#+id/group1"
android:checkableBehavior="single"
>
<item android:title="SUBTITLE1.1" />
<item android:title="SUBTITLE1.2" />
<item android:title="SUBTITLE1.3" />
</group>
</menu>
</item>
<item
android:id="#+id/action_vendors"
android:title="TITLE2">
<menu>
<group
android:id="#+id/group2"
android:checkableBehavior="single"
android:visible="false">
<item android:title="SUBTITLE2.1" />
<item android:title="SUBTITLE2.2" />
<item android:title="SUBTITLE2.3" />
</group>
</menu>
</item>
But if I keep any group in the xml android:visible="false" it doesn't appear in the drawer.
I also tried programatically showing/hiding the submenu with:
subMenu1.setGroupVisible(R.id.group1,false);
But it just hides the title menu also.
Is it possible with android.support.design.widget.NavigationView or do I have to use a custom view?
Menu menu = navigationView.getMenu();
for (int i = 0; i < menu.size(); i++) {
if (menu.getItem(i).getItemId() != R.id.login){
menu.getItem(i).setVisible(false);
}
}
I managed to show/hide the subtitles by making the titles a separate group. Not sure if this is the right way:
<group>
<item
android:id="#+id/action_venues"
android:title="group1" />
</group>
<group
android:id="#+id/group2"
android:checkableBehavior="single">
<item android:title="SUBTITLE1.1" />
<item android:title="SUBTITLE1.2" />
<item android:title="SUBTITLE1.3" />
</group>
<group>
<item
android:id="#+id/action_vendors"
android:title="group3" />
</group>
<group
android:id="#+id/group4"
android:checkableBehavior="single"
android:visible="false">
<item android:title="SUBTITLE2.1" />
<item android:title="SUBTITLE2.2" />
<item android:title="SUBTITLE2.3" />
</group>
And then in the code:
Menu drawerMenu = drawer.getMenu();
case R.id.action_venues:
drawerMenu.setGroupVisible(R.id.group2,true);
case R.id.action_vendors:
drawerMenu.setGroupVisible(R.id.group4,true);
But still can't animate.
For the group tags I used
navigationView.getMenu().setGroupVisible(false)
For the item tags I used
navigationView.getMenu().findViewById(R.id.id_of_the_item).setVisibility(View.GONE)
All working.
navigationView.getMenu().findItem(R.id.id_of_the_item).setVisible(false)

Categories

Resources