How to Create a Simple Divider in the New Navigationview

How to create a simple divider in the new NavigationView?

All you need to do is define a group with an unique ID, I have checked the implementation if group has different id's it will create a divider.

Example menu, creating the separator:

<menu 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"
tools:context=".MainActivity">

<group android:id="@+id/grp1" android:checkableBehavior="single" >
<item
android:id="@+id/navigation_item_1"
android:checked="true"
android:icon="@drawable/ic_home"
android:title="@string/navigation_item_1" />
</group>

<group android:id="@+id/grp2" android:checkableBehavior="single" >
<item
android:id="@+id/navigation_item_2"
android:icon="@drawable/ic_home"
android:title="@string/navigation_item_2" />
</group>
</menu>

Add one section separator for Navigation Drawer in Android

Try this....

    <group>
<item android:title="Express">
</item>
</group>

<group
android:id="@+id/grpid1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_balance_transfer"
android:icon="@mipmap/ic_launcher"
android:title="Balance Transfer" />

<item
android:id="@+id/nav_load_money"
android:icon="@mipmap/ic_launcher"
android:title="Load Money" />
<item
android:id="@+id/nav_report"
android:icon="@mipmap/ic_launcher"
android:title="Report" />
</group>

<group>
<item android:title="My Information">

</item>
</group>

<group
android:id="@+id/grpid2"
android:checkableBehavior="none">
<item
android:id="@+id/nav_profile"
android:icon="@mipmap/ic_launcher"
android:title="My Account" />
<item
android:id="@+id/nav_changePassword"
android:icon="@mipmap/ic_launcher"
android:title="Change Password" />
<item
android:id="@+id/nav_ViewUser"
android:icon="@mipmap/ic_launcher"
android:title="View User" />
<item
android:id="@+id/nav_addUser"
android:icon="@mipmap/ic_launcher"
android:title="Add User" />
<item
android:id="@+id/nav_addScheme"
android:icon="@mipmap/ic_launcher"
android:title="Add Scheme" />

<item
android:id="@+id/nav_logout"
android:icon="@mipmap/ic_launcher"
android:title="Log Out" />

</group>

</menu>

It looks like you just need to give your group tags unique ID's.

<group android:id="@+id/ids">
<!-- Divider will appear above this item -->
<item ... />
</group>

Sample Image

Sure @Tufan I can solve this problem also

Do this... put these lines to your dimen.xml

<dimen name="design_navigation_padding_top_default" tools:override="true">0dp</dimen>
<dimen name="design_navigation_separator_vertical_padding" tools:override="true">0dp</dimen>
<dimen name="design_navigation_padding_bottom" tools:override="true">0dp</dimen>

dimen.xml

<resources
xmlns:tools="http://schemas.android.com/tools"
>

<dimen name="design_navigation_padding_top_default" tools:override="true">0dp</dimen>
<dimen name="design_navigation_separator_vertical_padding" tools:override="true">0dp</dimen>
<dimen name="design_navigation_padding_bottom" tools:override="true">0dp</dimen>
<dimen name="navigation_separator_vertical_padding">0dp</dimen>

</resources>

New Screenshot remove padding by add this..

Sample Image

create divider between header and menu in navigationview for drawerlayout

I don't know what's the proper way to do it, but I have some workaround for it:

  1. As mentioned by P. Ilyin, you can put the divider on the bottom of your header view.

  2. You can add the divider onto the NavigationView layout, and manually adjusting the divider position.

    Example:

    <android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_menu_header"
    app:menu="@menu/menu_drawer">

    <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/background_gray"
    android:layout_marginTop="140dp"/>

    </android.support.design.widget.NavigationView>

    In this case, we make a custom gray line divider that has 1dp height, and it positioned under the menu header (140dp is the height of this menu header).

Android NavigationView menu group divider

Just give a unique id to each group. It will create a separator automatically.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/group_feature"
android:checkableBehavior="single">
<item android:id="@+id/navdrawer_item_map"
android:checked="true"
android:icon="@drawable/ic_drawer_map"
android:title="@string/navdrawer_item_map"/>

</group>
<group android:id="@+id/group_settings"
android:checkableBehavior="single">
<item android:id="@+id/navdrawer_item_settings"
android:icon="@drawable/ic_drawer_settings"
android:title="@string/navdrawer_item_settings"/>

</group>
</menu>

Navigation Drawer item divider with selected background color

I got it. It was a simple issue. I was just using the wrong state. So, here's the solution. I created two different layer lists (to add the divider to the navigation items):

nav_divider:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="@dimen/activity_horizontal_margin">
<shape android:shape="rectangle">
<solid android:color="@color/light_gray"/>
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
</shape>
</item>
</layer-list>

nav_divider_selected:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:left="@dimen/activity_horizontal_margin">
<shape android:shape="rectangle">
<solid android:color="@color/light_gray"/>
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="@color/light_blue"/>
</shape>
</item>
</layer-list>

Then, created a drawer_item_background drawable like this (state_checked should be used not state_selected):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/nav_divider_selected" android:state_checked="true"/>
<item android:drawable="@drawable/nav_divider"/>
</selector>

And, then I used this drawable as itemBackground in my NavigationView:

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
android:background="@color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu"
app:itemTextColor="@color/drawer_item"
app:itemIconTint="@color/drawer_item"
app:itemBackground="@drawable/drawer_item_background"/>

How to add divider in the bottom navigation view

If I'm not wrong, the bottom navigation view has been created to match/follow Material Design guidelines and dividers are not supposed to be part of bottom navigation.
https://material.io/design/components/bottom-navigation.html

Divider in Navigation Drawer using a xml menu

To add a divider after each menu item provide unique id to each group item as shown below


<group
android:id="@+id/group_item_1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_agreement"
android:icon="@mipmap/ic_launcher"
android:title="Agreement" />
</group>
<group
android:id="@+id/group_item_2"
android:checkableBehavior="single">
<item
android:id="@+id/nav_aboutus"
android:icon="@mipmap/ic_launcher"
android:title="About Us" />
</group>
<group
android:id="@+id/group_item_3"
android:checkableBehavior="single">
<item
android:id="@+id/nav_terms"
android:icon="@mipmap/ic_launcher"
android:title="Terms Condition " />
</group>
<group
android:id="@+id/group_item_4"
android:checkableBehavior="single">
<item
android:id="@+id/nav_chngpassword"
android:icon="@mipmap/ic_launcher"
android:title="Change Password" />
</group>
<group
android:id="@+id/group_item_5"
android:checkableBehavior="single">
<item
android:id="@+id/nav_signout"
android:icon="@mipmap/ic_launcher"
android:title="Sign Out" />
</group>

How to add one section separator for Navigation Drawer in Android?

Make sure you define each group with a unique ID, separator won't appear without the ID.

For example, this is my drawer_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/menu_top"
android:checkableBehavior="single">
<item
android:checked="true"
android:id="@+id/drawer_item_timeline"
android:icon="@drawable/ic_timer_grey600_24dp"
android:title="@string/drawer_timeline"/>
<item
android:id="@+id/drawer_item_reports"
android:icon="@drawable/ic_timetable_grey600_24dp"
android:title="@string/drawer_reports"/>
</group>

<group
android:id="@+id/menu_bottom"
android:checkableBehavior="none">

<item
android:id="@+id/drawer_item_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="@string/drawer_settings" >
</item>
</group>
</menu>

Sample drawer

Gabriel adds below in the comments that if the group doesn't have an id, the separator will not appear.



Related Topics



Leave a reply



Submit