Android menu with dividers and labels - android

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>

Related

Navigation menu items with dropdown menu Android

I'm using Navigation Drawer. I've used a spinner layout in a menu item, but it looks bad. I'm trying to have a view like this, but IDK how.
So when I press PRODUCT, a dropdown submenu appears.
I've tried this code to do so but it's not working.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="navigation_view">
<group android:checkableBehavior="single"
>
<item
android:id="#+id/nav_home"
android:checkable="true"
android:icon="#drawable/ic_outline_home_24"
android:title="#string/home" />
<item
android:id="#+id/nav_products"
android:checkable="true"
android:visible="true"
android:actionLayout="#android:layout/simple_spinner_dropdown_item"
android:icon="#drawable/ic_outline_shopping_bag_24"
app:showAsAction="collapseActionView"
android:title="#string/products" >
<menu>
<item
android:id="#+id/fire_alam"
android:title="#string/fire_alarms"
android:checkable="true"
app:showAsAction="never"
/>
<item
android:id="#+id/fire_fighting"
android:checkable="true"
app:showAsAction="never"
android:title="#string/fire_fighting"/>
</menu>
</item>
<item
android:id="#+id/nav_serv"
android:checkable="true"
android:icon="#drawable/ic_outline_room_service_24"
android:title="#string/services" />
</group>
</menu>
You can use ExpandableListView
Check:- https://www.journaldev.com/9942/android-expandablelistview-example-tutorial
Also you can use it in NavigationView
Check:-
https://www.journaldev.com/19375/android-expandablelistview-navigationview

Draw line between menu items

I am not using a navigation drawer .
I only am using a menu.xml and i want to draw a line separating between these items.
I am sure that the line is drawn but i think its color is white so i cannot see it , how can i change its color?
Thank you
<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:context="com.orangelabs.crypto.blindstorage.android.poc.HomeActivity">
<group android:checkableBehavior="none"
android:id="#+id/group1">
<item android:id="#+id/action_disconnect"
android:title="#string/action_disconnect"
app:showAsAction="never"/>
<item android:id="#+id/action_upload"
android:title="#string/action_upload"
app:showAsAction="never">
<menu>
<item android:id="#+id/action_chiffre"
android:title="#string/action_chiffre"
app:showAsAction="never"/>
<item android:id="#+id/action_non_chiffre"
android:title="#string/action_non_chiffre"
app:showAsAction="never"/>
</menu>
</item>
<item android:id="#+id/action_create_folder"
android:title="#string/action_create_folder"
app:showAsAction="never"/>
</group>
<group android:checkableBehavior="none"
android:id="#+id/group2">
<item android:id="#+id/action_logs"
android:title="#string/a_propos"
app:showAsAction="never"/>
</group>
</menu>
<View
android:height="1dp"
android:width="match_parent"
android: background="#android:color/grey"
/>
This is for navigation drawer, use this code in between the items.
For menu items, check this link:
How to add line divider for menu item Android
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#android:color/darker_gray"
/>
Use this code inbetween the menu items.

Menu items get added outside of the group

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>

Remove subsection and from navigation drawer submenu

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)

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)

Categories

Resources