Skip to content Skip to sidebar Skip to footer

Left Margin Between Icons And NavigationView

I have to add a left margin between the icons and the NavigationView, in arrow in the image bellow: I know that according to google specs, this margin must have 16dp but I need t

Solution 1:

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


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

Post a Comment for "Left Margin Between Icons And NavigationView"