Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overcome this item padding in navigation drawer?

I check out all the question and also google a lot I just want to remove this padding between each item in navigation view. Help me to sort out this problem thanks in advance.

This is the code of my main_drawer

<?xml version="1.0" encoding="utf-8"?>
<menu  xmlns:android="http://schemas.android.com/apk/res/android">


    <group android:checkableBehavior="single" android:id="@+id/home1"
        >
        <item
            android:id="@+id/home"
            android:title="Home"

            />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/aboutus1">
        <item

            android:id="@+id/nav_camera"


            android:title="AboutUs" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/Services1">
        <item
            android:id="@+id/nav_gallery"

            android:title="Services" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/consultation1">

        <item
            android:id="@+id/nav_slideshow"

            android:title="Consultation" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/gallery1">
        <item
            android:id="@+id/nav_manage"

            android:title="Gallery" />

        </group>
        <group android:checkableBehavior="single" android:id="@+id/appoinment1">
        <item
            android:id="@+id/nav_manage1"

            android:title="Appoinment" />
        </group>
    <group android:checkableBehavior="single" android:id="@+id/Contact_Us1">
        <item
            android:id="@+id/Contact_Us"

            android:title="Contact Us" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

My image is ...Want to remove the padding showing in blue mark

like image 466
Abhishek Bhardwaj Avatar asked Oct 07 '16 09:10

Abhishek Bhardwaj


People also ask

What's a navigation drawer?

The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.

What is drawer in ui?

Navigation drawers provide access to destinations and app functionality, such as switching accounts. They can either be permanently on-screen or controlled by a navigation menu icon. Navigation drawers are recommended for: Apps with five or more top-level destinations.

How do persistent navigation drawers work?

Persistent navigation drawers can toggle open or closed. The drawer sits on the same surface elevation as the content. It is closed by default and opens by selecting the menu icon, and stays open until closed by the user.

How do I view the navigation drawer?

The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.

Where are the dividers in the navigation drawer?

All dividers in the nav drawer are full-bleed within the drawer with 8dp padding above and below each divider. The navigation drawer scrolls in the same way a view scrolls. Settings and support are located at the bottom of the scrolling list, inline with the rest of the list content.

What is the new material design style navigation drawer?

With the release of Android 5.0 Lollipop, the new material design style navigation drawer spans the full height of the screen and is displayed over the ActionBar and overlaps the translucent StatusBar. Read the material design style navigation drawer document for specs on styling your navigation drawer.


2 Answers

According to source code of NavigationView found here, it led me to NavigationMenuPresenter (found here) which says, every normal type in menu list inflates R.layout.design_navigation_item. So if you preview it (here) you will notice what preference it uses.

<android.support.design.internal.NavigationMenuItemView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="?attr/listPreferredItemHeightSmall"
        android:paddingLeft="?attr/listPreferredItemPaddingLeft"
        android:paddingRight="?attr/listPreferredItemPaddingRight"
        android:foreground="?attr/selectableItemBackground"
android:focusable="true"/>

So, the final step is to override the style attribute, i.e. layout_height which references to "?attr/listPreferredItemHeightSmall" (default 48dp).

Open your styles.xml and override it by i.e using custom value:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- HERE-->
        <item name="listPreferredItemHeightSmall">18dp</item>
    </style>

Original:

enter image description here

Custom:

enter image description here

like image 139
deadfish Avatar answered Oct 16 '22 13:10

deadfish


Add this line to your dimens.xml file and customize this DP as per your need i have solved my problem by this lines.

<dimen name="design_navigation_padding_top_default" tools:override="true">5dp</dimen>
<dimen name="design_navigation_separator_vertical_padding" tools:override="true">0dp</dimen>
<dimen name="design_navigation_padding_bottom" tools:override="true">5dp</dimen>
<dimen name="design_navigation_icon_size" tools:override="true">20dp</dimen>
<dimen name="design_navigation_icon_padding" tools:override="true">12dp</dimen>
like image 40
Vivek Barai Avatar answered Oct 16 '22 15:10

Vivek Barai