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>
Related
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 already checked some questions but none has a really answer of what I'm looking for.
Basically I'm trying to add an item child to an item parent, or simulate a tree of options in the lateral panel, something like the Google Play app does:
I tried this as a test in my XML file:
<group android:checkableBehavior="single">
<item
android:orderInCategory="0"
android:id="#+id/nav_back"
android:icon="#drawable/ic_arrow_back_black_24dp"
android:title="Regresar a la pagina principal"
/>
</group>
<group android:checkableBehavior="single"
android:id="#+id/groupTypes">
<item
android:orderInCategory="1"
android:id="#+id/nav_negocios"
android:icon="#drawable/ic_local_convenience_store_black_48dp"
android:title="#string/negocios" />
<item
android:orderInCategory="1"
android:id="#+id/nav_hoteles"
android:icon="#drawable/ic_local_hotel_black_48dp"
android:title="#string/hoteles" />
<item
android:orderInCategory="1"
android:id="#+id/nav_bares"
android:icon="#drawable/ic_local_bar_black_48dp"
android:title="#string/bares">
<menu>
<item
android:orderInCategory="1"
android:id="#+id/nav_barType1"
android:icon="#drawable/ic_local_bar_black_48dp"
android:title="#string/restaurant" />
</menu>
</item>
<item
android:orderInCategory="1"
android:id="#+id/nav_restaurant"
android:icon="#drawable/ic_restaurant_black_48dp"
android:title="#string/restaurant" />
<item
android:orderInCategory="1"
android:id="#+id/nav_destacado"
android:icon="#drawable/ic_stars_black_48dp"
android:title="#string/destacado" />
</group>
<group android:title="#string/grupo2">
<menu>
<item
android:id="#+id/nav_taxi"
android:orderInCategory="2"
android:icon="#drawable/ic_local_taxi_black_48dp"
android:title="#string/mobility" />
</menu>
</group>
And this is what I got:
The "Bares" item has changed to a category and the item inside is just adding as the original sequence in the group.
Also I tried using the android:menuCategory="container" and android:menuCategory="secondary"options but the result is the same.
<item
android:orderInCategory="1"
android:id="#+id/nav_bares"
android:menuCategory="container"
android:icon="#drawable/ic_local_bar_black_48dp"
android:title="#string/bares">
<menu>
<item
android:orderInCategory="1"
android:id="#+id/nav_barType1"
android:menuCategory="secondary"
android:icon="#drawable/ic_local_bar_black_48dp"
android:title="#string/restaurant" />
</menu>
</item>
Any suggestions?
I couldn't find such an API in source codes. I decompiled Play Store apk file (version 7.7.17.O) with androidtool.
main.xml
<com.google.android.finsky.layout.play.FinskyDrawerLayout>
<com.google.android.finsky.layout.InsetsFrameLayout>
</com.google.android.finsky.layout.InsetsFrameLayout>
<include layout="#layout/play_drawer_container" />
</com.google.android.finsky.layout.play.FinskyDrawerLayout>
play_drawer_container.xml
<LinearLayout>
<ListView android:id="#id/play_drawer_list" style="#style/PlayDrawerList" />
<TextView android:id="#id/play_drawer_docked_action" android:visibility="gone" style="#style/PlayDrawerDockedAction" />
</LinearLayout>
As you can see, menu items are not set via app:menu. It's just a ListView.
You can also see the difference of view bounds when "Show layout bounds" is turned on:
If they have used the default menu items, you should have seen the boundaries matching.
So, answering to your question: it can be implemented with custom approach, menus have no such functionality.
Update
There are a number of ways to implement that kind of functionality:
Using RecyclerView
Using ListView
Using ScrollView
If there are few items in the list, which won't need recycling functionality, than you can stick with ScrollView. Otherwise, prefer either RecyclerView or ListView, where each of item has its own row item xml.
You need to surround your item with the menu, and then add the submenu below it. It might look like this:
<group android:checkableBehavior="single">
<item
android:orderInCategory="0"
android:id="#+id/nav_back"
android:icon="#drawable/ic_arrow_back_black_24dp"
android:title="Regresar a la pagina principal"
/>
</group>
<group android:checkableBehavior="single"
android:id="#+id/groupTypes">
<menu>
<item
android:orderInCategory="1"
android:id="#+id/nav_negocios"
android:icon="#drawable/ic_local_convenience_store_black_48dp"
android:title="#string/negocios" />
<item
android:orderInCategory="1"
android:id="#+id/nav_hoteles"
android:icon="#drawable/ic_local_hotel_black_48dp"
android:title="#string/hoteles" />
<item
android:orderInCategory="1"
android:id="#+id/nav_bares"
android:icon="#drawable/ic_local_bar_black_48dp"
android:title="#string/bares">
<menu>
<item
android:orderInCategory="1"
android:id="#+id/nav_barType1"
android:icon="#drawable/ic_local_bar_black_48dp"
android:title="#string/restaurant" />
</menu>
</item>
<item
android:orderInCategory="1"
android:id="#+id/nav_restaurant"
android:icon="#drawable/ic_restaurant_black_48dp"
android:title="#string/restaurant" />
<item
android:orderInCategory="1"
android:id="#+id/nav_destacado"
android:icon="#drawable/ic_stars_black_48dp"
android:title="#string/destacado" />
</menu>
</group>
<group android:title="#string/grupo2">
<menu>
<item
android:id="#+id/nav_taxi"
android:orderInCategory="2"
android:icon="#drawable/ic_local_taxi_black_48dp"
android:title="#string/mobility" />
</menu>
</group>
You can try with drop down menu or expandable list or simple lists which can be seen on click of menu options accordingly in navigation drawer instead of group.
For your first approach, you can make a little edit. That SubItem is also an item an instead of putting everything in group you should put in an item. Try this example below:
<item
android:id="#+id/nav_category_electronics"
android:icon="#drawable/ic_email_black_24dp"
android:title="Electronics">
<menu>
<item
android:id="#+id/nav_subcategory_laptops"
android:icon="#drawable/laptop"
android:title="Laptops" />
<item
android:id="#+id/nav_subcategory_tablet"
android:icon="#drawable/tablet3"
android:title="Tablets" />
<item
android:id="#+id/nav_category_phones"
android:icon="#drawable/smartphone"
android:title="Mobile Phones" />
<item
android:id="#+id/nav_subcategory_gadgets"
android:icon="#drawable/ic_developer_mode_black_18dp"
android:title="Gadgets" />
</menu>
</item>
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
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)
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>