How I can remove the unnecessary top padding of the Navigation view?

19,512

Solution 1

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

Solution 2

You can override predefined dimensions at your dimens.xml as;

<dimen name="design_navigation_padding_top_default" tools:override="true">0dp</dimen>
<dimen name="design_navigation_separator_vertical_padding" tools:override="true">0dp</dimen>
<dimen name="design_navigation_padding_bottom" tools:override="true">0dp</dimen>

Other possible values are here: https://github.com/android/platform_frameworks_support/blob/master/design/res/values/dimens.xml

Solution 3

You should read the Ian's answer. The NavigationView follows the Material guidelines and you should not break these specs.

However, currently, you can override this value in your project.

Just add to your dimens.xml:

<dimen name="design_navigation_separator_vertical_padding">0dp</dimen>

Warning:

  • in the future the Android Team can change this value!
  • the same value is applied to each separator in the list

The NavigationView uses a LinearLayout as header view. You can see this layout in the source code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      android:paddingBottom="@dimen/design_navigation_separator_vertical_padding" />

Solution 4

very simple step : add the following attribute to your base application theme (stylename=AppTheme) Style.xml :

        <item name="listPreferredItemHeightSmall">18dp</item>
Share:
19,512
Ahmed Talaat
Author by

Ahmed Talaat

Updated on June 22, 2022

Comments

  • Ahmed Talaat
    Ahmed Talaat almost 2 years

    There's an unnecessary top padding between the header and the first item shown in this picture.

    enter image description here

    How it can be removed?

    you can find the source code here: https://github.com/chrisbanes/cheesesquare

  • Gabriele Mariotti
    Gabriele Mariotti almost 9 years
    You can override this value easily. May be this padding can be added programmatically to avoid this case.
  • Gabriele Mariotti
    Gabriele Mariotti over 8 years
    @headsvk Thanks. Updated the answer
  • Uğur Tılıkoğlu
    Uğur Tılıkoğlu about 7 years
    @kishorejethava could you please check the resource files under the generated folder? it may be the classic "clean - rebuild" case
  • superUser
    superUser over 6 years