Logo Questions Linux Laravel Mysql Ubuntu Git 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>

Navigation drawer android

Thanks.

like image 629
awaistoor Avatar asked Jan 07 '23 21:01

awaistoor


2 Answers

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>
like image 176
Eugene H Avatar answered Jan 09 '23 12:01

Eugene H


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

like image 23
piotrek1543 Avatar answered Jan 09 '23 10:01

piotrek1543