3

I have to add a left margin between the icons and the NavigationView, in arrow in the image bellow:

enter image description here

I know that according to google specs, this margin must have 16dp but I need to change it. I have tried:

 <dimen tools:override="true" name="design_navigation_icon_padding">64dp</dimen>
 <dimen tools:override="true" name="design_navigation_separator_vertical_padding">20dp</dimen>

But still not working. Any ideas?

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
  • http://stackoverflow.com/questions/36397268/android-navigationview-reduce-space-between-icon-and-text-and-itembackground – Quick learner May 11 '17 at 15:22
  • You can try using these steps to override it: http://stackoverflow.com/questions/39913570/how-to-overcome-this-item-padding-in-navigation-drawer – David Shinabarger May 11 '17 at 15:23

2 Answers2

5

The xml layout of that item is design_navigation_item.xml

<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"/>

As you can see, paddings that are applied are taken from the activity's theme - listPreferredItemPaddingLeft and listPreferredItemPaddingRight. Thus, you have to apply your custom theme to NavigationView overriding those attributes with necessary values.

In styles.xml:

<style name="MyNavigationViewItemStyle" parent="AppTheme">
    <item name="listPreferredItemPaddingLeft">0dp</item>
    <item name="listPreferredItemPaddingRight">0dp</item>
</style>

We want to change only those two attributes from activity's theme, thus we are extending the theme, that is applied to the activity.

In layout xml:

<android.support.design.widget.NavigationView
    ...
    app:theme="@style/MyNavigationViewItemStyle"/>

Result

enter image description here

azizbekian
  • 60,783
  • 13
  • 169
  • 249
2

For my case I added one line to xml: app:itemHorizontalPadding="@dimen/horizontal_padding_on_nav_drawer"

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    android:clipToPadding="false"
    android:paddingBottom="@dimen/space48"
    app:headerLayout="@layout/layout_nav_header"
    app:insetForeground="@color/black"
    app:itemBackground="@drawable/background_selected_menu"
    **app:itemHorizontalPadding="@dimen/horizontal_padding_on_nav_drawer"**
    app:menu="@menu/drawer">