Niubility
Niubility

Reputation: 577

Set the distance between the menu in a navigation drawer and the screen edge?

I want to set the distance "d" to a specific value, how can I make it?

enter image description here

Yes, the question How to programatically set the margin between icon and title of menu item of Navigation Drawer in Android? is just the same as mine. My code is as follows:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_main"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_product"
            android:icon="@drawable/ic_product"
            android:title="Products" />
        <item
            android:id="@+id/nav_statistic"
            android:icon="@drawable/ic_statistic"
            android:title="Statistic" />
        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_settings"
            android:title="Settings" />
        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/ic_logout"
            android:title="Log out" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

Upvotes: 2

Views: 2413

Answers (1)

Hamed Jaliliani
Hamed Jaliliani

Reputation: 2929

It worked for me:)

In NavigationView for Padding between the screen and the icon menu :

app:itemHorizontalPadding="40dp"

and for padding between the menu icon and the text use this:

app:itemIconPadding="28dp"

full code:

<com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/background_light"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer"
            app:itemTextColor="?android:textColorPrimary"
            app:itemIconPadding="28dp"
            app:itemHorizontalPadding="40dp"  >

Upvotes: 3

Related Questions