5

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>

Navigation drawer android

Thanks.

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
awaistoor
  • 786
  • 2
  • 11
  • 36

3 Answers3

7

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>
Eugene H
  • 3,520
  • 3
  • 22
  • 39
  • 1
    Thanks man. The problem was I wasn't adding the group unique ids. Adding the ids does the trick. Thanks!! – awaistoor Jan 07 '16 at 22:01
2

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.

material design specs

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

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • First of all, thanks for your detailed answer. I have followed your reply and the confusing thing is that now submenu is getting the title from the item's title above it. e.g. submenu's title is 'Others' which is above it, if change the item's name, the value for submenu's header changes to :S – awaistoor Jan 07 '16 at 00:28
  • it seems to be that is necessary to give a submenu name. Delete this ``, i mean do that all items are in the same menu - no submenus. Then make a bottom border line using style and add to `others` item. It should work – piotrek1543 Jan 07 '16 at 08:03
  • About styling items, you would find here: http://stackoverflow.com/questions/2944244/change-the-background-color-of-the-options-menu – piotrek1543 Jan 07 '16 at 08:04
  • I just want to add a simple line in two menus .. the link you suggested is for styling whole menu and not navigational drawer menu, the other menu. – awaistoor Jan 07 '16 at 21:45
  • Because you need to add a style to item `Others`, which style would add bottom line. I dont think that you can add between these items this android:layout_height="1dp"; android:layout_width="match_parent"; android:background="#0000000000 this would draw a line – piotrek1543 Jan 07 '16 at 21:51
  • 2
    See the accepted answer. The problem was I wasn't adding the group unique Ids. Adding unique ids create the separator for two different groups. Still thanks alot for your help :) – awaistoor Jan 07 '16 at 22:02
0

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)

Steven L
  • 15,304
  • 1
  • 21
  • 16