Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationView Items Padding in Android

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>
like image 677
Sumit Shukla Avatar asked Sep 29 '17 04:09

Sumit Shukla


2 Answers

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" />`
like image 63
Alejandro Avatar answered Oct 06 '22 01:10

Alejandro


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

like image 38
Gabriele Mariotti Avatar answered Oct 06 '22 00:10

Gabriele Mariotti