6

How do I set padding for items inside NavigationView.I need to set paddingLeft for items as I have removed icons.What i have tried is this but it is not working:

<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:focusable="true"
    android:foreground="?attr/selectableItemBackground"
    app:headerLayout="@layout/nav_header_navigation_drawer"
    app:itemBackground="@drawable/selected_background"
    app:itemTextAppearance="@style/Drawer"
    app:menu="@menu/activity_navigation_drawer_drawer" />

My Drawer.xml file in styles:

<style name="Drawer">
    <item name="android:listPreferredItemPaddingLeft">50dp</item>
    <item name="android:textSize">30sp</item>
    <item name="android:typeface">serif</item>
</style>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57

4 Answers4

10

In case anyone wants to adjust the padding for the items in the NavigationDrawer using the latest material design, checkout these new attributes app:itemIconPadding and app:itemHorizontalPadding.

app:itemIconPadding will adjust the padding between your icon and the item's text app:itemHorizontalPadding, as its name suggest, will adjust the padding from the left and right horizontally for the item.

<com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"

            app:itemIconPadding="16dp"
            app:itemHorizontalPadding="10dp"

            app:menu="@menu/nav_drawer_menu" />`
Alejandro
  • 325
  • 4
  • 10
5

Since you are using a menu without icons you can use the app:itemHorizontalPadding attribute:

<com.google.android.material.navigation.NavigationView
    app:itemHorizontalPadding="2dp"
    ../>

The result with default value and custom value:

enter image description here enter image description here

Use the app:itemShapeInsetStart="0dp" to change the margin of the shape:

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • hi, this is unrelated but how can I achieve that active indicator that has margins and rounded corners – Gilbert Mendoza Jun 23 '20 at 08:02
  • 1
    @GilbertMendoza Check this answer: https://stackoverflow.com/questions/55933739/add-custom-shape-ripple-with-selector-on-navigationview-menu-item/58683034#58683034 – Gabriele Mariotti Jun 23 '20 at 08:46
4

you can change "listPreferredItemHeightSmall" value for changing navigation item vertical padding in your application theme

<!-- Your application theme -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <!-- THIS LINE-->
    <item name="listPreferredItemHeightSmall">54dp</item>
</style>

(default value is 48dp)

0

You can add this value ion dimension file under the res->values->dimension folder and add the following line of code.

<dimen name="design_navigation_icon_padding" tools:override="true">8dp</dimen>
Sardar Khan
  • 845
  • 6
  • 16