Java – The left margin between the icon and the NavigationView

The left margin between the icon and the NavigationView… here is a solution to the problem.

The left margin between the icon and the NavigationView

I have to add a left margin between the icon and the NavigationView, as shown by the arrow in the following image:

enter image description here

I

know this margin must have 16dp according to google specs but I need to change it. I 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 it still doesn’t work. Any ideas?

Solution

The XML layout for the project 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, the app’s padding is taken from the activity’s theme – listPreferredItemPaddingLeft and listPreferredItemPaddingRight. Therefore, you must apply a custom theme to the NavigationView, overriding these properties with the 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 these two properties in the Activity theme, so we’re extending the theme that applies to the Activity.

In layout XML:

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

Result

enter image description here

Related Problems and Solutions