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