Add item to navigationView - android

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");

Related

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.

Adding an item programatically in navigation drawer menu

I have a navigation drawer in my application. I want to add an option programatically in the second section of the navigation drawer's menu. The menu.xml is-
<group android:checkableBehavior="single">
<item android:id="#+id/nav_connect_sensor"
android:icon="#drawable/ic_action_bluetooth"
android:title="Connect Bluetooth" />
<item android:id="#+id/nav_take_tour"
android:icon="#drawable/ic_action_video"
android:title="Take a tour" />
<item android:id="#+id/nav_my_stats"
android:icon="#drawable/ic_action_bargraph"
android:title="My Stats" />
</group>
<item android:title="#string/navigation_drawer_group_title"
android:id="#+id/other_labels">
<menu>
<item android:id="#+id/nav_myAccountSettings"
android:icon="#drawable/ic_settings"
android:title="My Account" />
<item android:id="#+id/nav_application_settings"
android:icon="#drawable/ic_action_settings"
android:title="Application Settings" />
<item android:id="#+id/comparison"
android:icon="#drawable/ic_swap_horiz"
android:title="Compare"/>
<item android:id="#+id/nav_help_and_feedback"
android:icon="#drawable/ic_live_help"
android:title="Help and Feedback" />
<item android:id="#+id/nav_logout"
android:icon="#drawable/ic_action_exit"
android:title="Sign Out" />
</menu>
</item>
For adding an option programatically-
Menu menu = mNavigationView.getMenu();
menu.add(0, Menu.FIRST, Menu.FIRST, "Players")
.setIcon(R.drawable.ic_people);
This option is getting added at the last. How can I add the option in the third position i.e. below Application Settings
Try this:
Menu.THIRD=3;
Menu menu = mNavigationView.getMenu();
menu.add(0, Menu.THIRD, 2, "Players")
.setIcon(R.drawable.music_audio);
You can use.
Menu menu = navView.getMenu(); // navView is NavigationView reference.
menu.add("Item 1");
menu.add("Item 2");
menu.add("Item 3");

Add items to menu group programatically in navigation view

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

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)

Android menu with dividers and labels

can I add a divider and items (labels) that is not selectable to my PopUp meny xml.
If want a meny like this
Label1
------------- (divider)
RadioButton1
RadioButton2
------------- (divider)
Item1
Item2
------------- (divider)
Item3
I create my popup menu with
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener((PopupMenu.OnMenuItemClickListener) this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.menu_popup, popup.getMenu());
popup.show();
Im using this xml for my menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".ComicBoothActivity">
<item android:id="#+id/Label1" android:title="Label1" />
<item android:id="#+id/Divider1" android:title="" android:showAsAction="always" android:actionLayout="#layout/menu_divider" />
<group android:checkableBehavior="single">
<item android:id="#+id/RadioButton1"
android:title="RadioButton1"
android:checked="true"/>
<item android:id="#+id/RadioButton2"
android:title="RadioButton2" />
</group>
<item android:id="#+id/Divider2" android:title="" android:showAsAction="always" android:actionLayout="#layout/menu_divider" />
<item android:id="#+id/Item1"
android:title="Item1" />
<item android:id="#+id/Item2"
android:title="Item2" />
<item android:id="#+id/Divider3" android:title="" android:showAsAction="always" android:actionLayout="#layout/menu_divider" />
<item android:id="#+id/Item2"
android:title="Item2" />
</menu>
And the divider layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="3dp"
android:background="#color/orange08">
</RelativeLayout>
How can I make the label "unselectable", i.e that nothing happens when you click it and the menu stays open. Is this possibe? The divider layout is not showing. Its only a empty item entry in the menu. What have I missed? Or is there a better way to add dividers?
<item android:title="unselectable">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#android:drawable/ic_menu_share"
android:title="Share"/>
</menu>
</item>

Categories

Resources