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
Related
Background
Just want to see how to make action items in code without XML
The problem
I've come to some case that I fail to see how I can create in code.
The case below is showing a normal action-item that has an icon, and when pressed, it shows a sub-menu of items to choose from. I want to create the same, but programmatically:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:icon="#android:drawable/ic_menu_info_details" android:title="More info" app:showAsAction="always">
<menu>
<group android:checkableBehavior="none" android:menuCategory="container">
<item android:id="#+id/..." android:title="item 1"/>
<item android:id="#+id/..." android:title="item 2"/>
<item android:id="#+id/..." android:title="item 3"/>
</group>
</menu>
</item>
</menu>
When using this, the result is:
And after clicking:
What I've tried
I tried 2 solutions:
Create a subMenu (using addSubMenu), and then setting an action-item properties to it.
Create a normal Action item (using add) and then adding sub-items to it.
The result for both of them failed in comparison to what
The question
How can I make the exact same thing programmatically, without using XML resource for it.
I want to show navigation menu item on fragment activity. there are 4 item of navigation item . I want to show it on fragment with list view. i want to show on both navigation menu and fragment activity. after click showing same page individually.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/adviser"
android:icon="#drawable/ic_person"
android:title="Adviser" />
<item
android:id="#+id/executive"
android:icon="#drawable/ic_group"
android:title="#string/exce" />
<item
android:id="#+id/member"
android:icon="#drawable/ic_people"
android:title="#string/mem" />
<item
android:id="#+id/alumni"
android:icon="#drawable/ic_school"
android:title="#string/alu" />
</group>
<item android:title="Informations">
<menu>
<item
android:id="#+id/aboutrudf"
android:icon="#drawable/ic_info"
android:title="#string/abo" />
</menu>
</item>
</menu>
there's template for creating new activity click new > activity
I'd like to move the horizontal separator that separates between different item groups in a navigation drawer and put it next to the titles of the group.
let's say I have the following XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/mainFragment"
android:title="Principal" />
</group>
<item android:title="Second menu">
<menu>
<group android:checkableBehavior="single">
<item
android:id="#+id/listOption"
android:title="List" />
<item
android:id="#+id/favoriteOption"
android:title="Favorite" />
</group>
</menu>
</item>
</menu>
which has the following result
I would like to get a result where the separating line is next to "Second Menu" in way that it's positioned in the center. like this
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.
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