I have a menu with groups like below. Is that possible to set a headers for each group? I know that's possible for submenus, but what about first level groups?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single" android:id="#+id/main_group">
<item
android:id="#+id/leagues_in_progress_item"
android:icon="#drawable/ic_format_line_spacing_black_48dp"
android:title="#string/title_activity_leagues_in_progress" />
<item
android:id="#+id/last_matches_item"
android:icon="#drawable/ic_access_alarm_black_48dp"
android:title="#string/title_activity_last_matches" />
</group>
<group android:checkableBehavior="single" android:id="#+id/another_group">
<item
android:id="#+id/archive_item1"
android:icon="#drawable/ic_folder_open_black_48dp"
android:title="#string/title_activity_archive" />
<item
android:id="#+id/put_score_item3"
android:icon="#drawable/ic_add_circle_outline_black_48dp"
android:title="#string/title_activity_put_score" />
</group>
</menu>
Yes, each group would be an item in your top-level tree. Then just set the title attribute for each of these. Using your code it would be like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="First group"
android:id="#+id/first_group">
<menu android:checkableBehavior="single" android:id="#+id/main_group">
<item
android:id="#+id/leagues_in_progress_item"
android:icon="#drawable/ic_format_line_spacing_black_48dp"
android:title="#string/title_activity_leagues_in_progress" />
<item
android:id="#+id/last_matches_item"
android:icon="#drawable/ic_access_alarm_black_48dp"
android:title="#string/title_activity_last_matches" />
</menu>
</item>
<item
android:title="Second group"
android:id="#+id/second_group">
<menu android:checkableBehavior="single" android:id="#+id/another_group">
<item
android:id="#+id/archive_item1"
android:icon="#drawable/ic_folder_open_black_48dp"
android:title="#string/title_activity_archive" />
<item
android:id="#+id/put_score_item3"
android:icon="#drawable/ic_add_circle_outline_black_48dp"
android:title="#string/title_activity_put_score" />
</menu>
</item>
</menu>
Related
My current problem is i want to move both my 'Log out' and 'About the app' to the left because both of them doesn't have any icon.
i'm currently finding a way to move the 'log out' and 'about the app' to adjust on the left so that they can be align with the icons.
Here is my navigation drawer menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item
android:id="#+id/drawer_home"
android:icon="#drawable/ic_drawer_home"
android:title="Home" />
<item
android:id="#+id/drawer_pay"
android:icon="#drawable/ic_drawer_pay"
android:title="Pay" />
<item
android:id="#+id/drawer_transfer"
android:icon="#drawable/ic_drawer_transfer"
android:title="Transfer" />
<item
android:id="#+id/drawer_load"
android:icon="#drawable/ic_drawer_load"
android:title="Load" />
<item
android:id="#+id/drawer_link"
android:icon="#drawable/ic_balance_card"
android:title="Link ATM Card" />
<item
android:id="#+id/drawer_merchants"
android:icon="#drawable/ic_drawer_merchants"
android:title="Locate Merchants" />
<item
android:id="#+id/drawer_logout"
android:title="Log Out" />
<item
android:id="#+id/drawer_about"
android:title="About the App" />
</group>
</menu>
Separate those sections you want to addjust to the left like below:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:checkableBehavior="single">
<item
android:id="#+id/drawer_home"
android:icon="#drawable/ic_drawer_home"
android:title="Home" />
<item
android:id="#+id/drawer_pay"
android:icon="#drawable/ic_drawer_pay"
android:title="Pay" />
<item
android:id="#+id/drawer_transfer"
android:icon="#drawable/ic_drawer_transfer"
android:title="Transfer" />
<item
android:id="#+id/drawer_load"
android:icon="#drawable/ic_drawer_load"
android:title="Load" />
<item
android:id="#+id/drawer_link"
android:icon="#drawable/ic_balance_card"
android:title="Link ATM Card" />
<item
android:id="#+id/drawer_merchants"
android:icon="#drawable/ic_drawer_merchants"
android:title="Locate Merchants" />
</group>
<item android:title="You title">
<menu>
<item
android:id="#+id/drawer_logout"
android:title="Log Out" />
<item
android:id="#+id/drawer_about"
android:title="About the App" />
</menu>
</item>
</menu>
I can easily show icons in the menu for the subitems but what if I wanted to shows icon for the category menu items?
I tried like the code example below but it simply ignores android:icon parameter and displays just text.
First screenshot is what I get and the next one how I would like to get it looking.
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<item
android:icon="#drawable/ic_lists"
android:title="#string/lists">
<menu>
<item
android:id="#+id/nav_my_lists"
android:title="#string/my_lists" />
<item
android:id="#+id/nav_list_search"
android:title="#string/search_and_apply" />
<item
android:id="#+id/nav_list_applications"
android:title="#string/applications" />
<item
android:id="#+id/nav_list_banned"
android:title="#string/banned" />
</menu>
</item>
<item
android:icon="#drawable/ic_feedback"
android:title="#string/feedback">
<menu>
<item
android:id="#+id/nav_feedback_from_others"
android:title="#string/from_others" />
<item
android:id="#+id/nav_feedback_to_leave"
android:title="#string/i_need_to_leave" />
<item
android:id="#+id/nav_my_feedback"
android:title="#string/i_have_left" />
<item
android:id="#+id/nav_feedback_info"
android:title="#string/banned" />
</menu>
</item>
<item android:title="#string/logout" />
Question is, if there is menu icon property why it doesn't get displayed for top category menu items?
Use the Below Code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:icon="#drawable/ic_lists"
android:title="#string/lists">
<item
android:id="#+id/nav_my_lists"
android:title="#string/my_lists"
/>
<item
android:id="#+id/nav_list_search"
android:title="#string/search_and_apply" />
<item
android:id="#+id/nav_list_applications"
android:title="#string/applications" />
<item
android:id="#+id/nav_list_banned"
android:title="#string/banned" />
<item
android:icon="#drawable/ic_feedback"
android:title="#string/feedback"/>
<item
android:id="#+id/nav_feedback_from_others"
android:title="#string/from_others" />
<item
android:id="#+id/nav_feedback_to_leave"
android:title="#string/i_need_to_leave" />
<item
android:id="#+id/nav_my_feedback"
android:title="#string/i_have_left" />
<item
android:id="#+id/nav_feedback_info"
android:title="#string/banned" />
</group>
I'm working on a demo of DrawerLayout with fragments in android,I've implemented it but I want to add subitems to items when it is clicked it should populate the subitems just like an expandable listView. I have found some thread with expandable ListView,But I don't want it,Please suggest me how can I do it?
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Customer">
<menu
>
<item
android:id="#+id/nav_cust_mgmt"
android:icon="#drawable/ic_group"
android:title="Customer Management" >
</item>
<item
android:id="#+id/nav_cust_opn"
android:icon="#drawable/ic_user"
android:title="Customer Operation" />
<item
android:id="#+id/nav_cust_tkt"
android:icon="#drawable/ic_page"
android:title="Customer Tickets" />
</menu>
</item>
<item android:title="LCO">
<menu>
<item
android:id="#+id/nav_dashboard"
android:icon="#drawable/ic_home"
android:title="Dashboard" />
<item
android:id="#+id/nav_stm"
android:icon="#drawable/ic_stmnt"
android:title="Statement of Account" />
<item
android:id="#+id/nav_staf_mgmnt"
android:icon="#drawable/ic_grup"
android:title="Staff Management" />
<item
android:id="#+id/nav_my_tkts"
android:icon="#drawable/ic_page"
android:title="My Tickets" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_logout"
android:title="Logout" />
</menu>
</item>
</menu>
I want to add subcategories to this.Please help me.
You can create groups in menu file like this
<item
android:id="#+id/nav_menuItem9"
android:title="#string/str_drawer_group1">
<menu android:checkableBehavior="all">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_cust_mgmt"
android:icon="#drawable/ic_group"
android:title="Customer Management" >
</item>
...
</group>
</menu>
</item>
OR you can put expandable list view in drawer part and then handle everything manually. for more about this check this link
I want to remove the unwanted space created in submenu.
Is there any possibility I can achieve it from menu.xml class?
Here is my navigation_drawer_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/all"
android:checked="true"
android:icon="#drawable/ic_channels"
android:title="All Channels" />
.......
<item
android:id="#+id/other"
android:icon="#drawable/ic_other"
android:title="Others" />
</group>
<item android:title="">
<menu>
<item
android:id="#+id/setting"
android:icon="#drawable/ic_action_settings"
android:title="Settings" />
<item
android:id="#+id/about"
android:icon="#drawable/ic_about"
android:title="About" />
<item
android:id="#+id/share"
android:icon="#drawable/ic_share"
android:title="Share" />
</menu>
</item>
Thanks.
This is how I implement it. Because you are adding a title to a menu it is not recognizing it is empty.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav1"
android:checked="true"
android:title="Pos 1" />
<item
android:id="#+id/nav2"
android:title="Pos 2" />
</group>
<group
android:id="#+id/menu_two"
android:checkableBehavior="single">
<item
android:id="#+id/pos3"
android:title="Pos 3" />
<item
android:id="#+id/pos4"
android:title="Pos 4" />
</group>
</menu>
First, please read this Ian Lake's (Android Developer Advocate) answer:
NavigationView seeks to match the material design specs for the
navigation drawer which state an 8dp space between content areas.
Generally there are no ways to override NavigationView to
specifically break the specifications.
From: How I can remove the unnecessary top padding of the Navigation view?
As you see this undesired padding is sub header of your section.
To delete it, you would need to delete this
<item android:title=""> and </item>
To do it you need ****merge** all menu elements** () into single group (no subsections).
After changes your code should look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/all"
android:checked="true"
android:icon="#drawable/ic_channels"
android:title="All Channels" />
.......
<item
android:id="#+id/other"
android:icon="#drawable/ic_other"
android:title="Others" />
<menu>
<item
android:id="#+id/setting"
android:icon="#drawable/ic_action_settings"
android:title="Settings" />
<item
android:id="#+id/about"
android:icon="#drawable/ic_about"
android:title="About" />
<item
android:id="#+id/share"
android:icon="#drawable/ic_share"
android:title="Share" />
<menu>
</group>
Still you can do this: How to create a custom navigation drawer in android
If you have a question, please free to ask
Hope it help
To accomplish this programatically, use the overload:
menu.add(int groupId, int itemId, int order, int titleRes)
(and make sure to provide different groupId's for the different sections)
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)