TeachMeJava
TeachMeJava

Reputation: 669

Set Margin to Navigation Drawer's Default Divider

I tried to set margin to default navigation drawer's "divider" attribute. I found many approach in Stackoverflow form but none of the answers are adequate.

I could create a custom navigation drawer list without using /menu/activity_main_drawer.xml. That looks like what I want. But I have concerns about the performance of the application.

I want to add margin to default divider. Is there a way to customize default navigation drawer divider?

That is my customized divider. Everything is perfect. enter image description here

That is the default navigation drawer's divider which I could not give any margin. enter image description here

style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    <item name="android:listDivider">@color/colorAccent</item>
...
</style>

divider_layout.xml

<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="60dp"
    android:insetRight="8dp" >

    <shape>
        <solid android:color="#c0c0c0" />
    </shape>

</inset>

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_camera"
            android:icon="@drawable/rakipicon"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/ic_menu_manage"
            android:title="Tools" />
    </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>

activity_main.xml

<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"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@drawable/divider_layout"
        app:menu="@menu/activity_main_drawer" />    
</android.support.v4.widget.DrawerLayout>

Upvotes: 5

Views: 3011

Answers (3)

Shogun Nassar
Shogun Nassar

Reputation: 633

Just add this in the drawer layout:

app:dividerInsetEnd="25dp"
app:dividerInsetStart="57dp"

Like below:

<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"
        app:dividerInsetEnd="25dp"
        app:dividerInsetStart="57dp"
        app:elevation="32dp"
        app:headerLayout="@layout/nav_header_main"
        app:itemIconTint="@color/gray"
        app:itemTextColor="@color/gray"
        app:menu="@menu/activity_main_drawer">

Upvotes: 0

giorgos.nl
giorgos.nl

Reputation: 2832

You can create a drawable named menu_divider.xml in your resources folder:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:drawable="#c0c0c0"
    android:left="16dp"
    android:right="16dp" />

and set your navigation view to use it:

<style name="ThemeOverlay.App.NavigationView" parent="">
    <item name="android:listDivider">@drawable/menu_divider</item>
</style>

Upvotes: 1

Mayu
Mayu

Reputation: 161

I know it's been a while, but for anyone that might stumble upon this, I had the same issue and resolved it like this. Where you define the navigation view, you can just set padding horizontal, which will set a padding for the entire navigation view. This will apply to the dividers as well.

    <com.google.android.material.navigation.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:paddingHorizontal="24dp">

Upvotes: 1

Related Questions