2

I need to add a divider between two NavigationViews in my app. (navigation_drawer_top and navigation_drawer_bottom).

I have tried this. But this added divider to top of the view. not at end of the first NavigationView(navigation_drawer_top).

<View android:layout_width="match_parent"
          android:layout_height="1dp"
          android:background="?android:attr/listDivider"/>

Here is my code

<?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/navigation_drawer_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer_top"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:background="@color/menuColor"
            android:paddingLeft="50dp"
            app:headerLayout="@layout/nav_header_main"
            app:itemTextAppearance="@style/NavigationDrawerStyle"
            app:itemTextColor="@color/menuTextColour"
            app:menu="@menu/menu_navigation_drawer_top"
            />

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/menuColor"
            android:layout_gravity="bottom"
            android:paddingLeft="50dp"
            app:itemTextAppearance="@style/NavigationDrawerStyle"
            app:itemTextColor="@color/menuTextColour"
            app:menu="@menu/menu_navigation_drawer_bottom" />

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

</android.support.v4.widget.DrawerLayout>
Bishan
  • 15,211
  • 52
  • 164
  • 258
  • You need to create the layout of navigation_drawer_bottom and the divider at the top of the layout..Try it and let me know in case of concern – Ravindra Kushwaha Mar 14 '17 at 07:20
  • http://stackoverflow.com/questions/30605286/navigationview-how-to-insert-divider-without-subgroup – Komal12 Mar 14 '17 at 07:21

2 Answers2

1

It's quite simple to do. All you need to do is make a group and put unique id to the groups. The unique id is what plays the real trick in adding a divider to your menu.

Here is a quick example for you,

<group android:id="@+id/group1" android:checkableBehavior="single" >
    <item
        android:id="@+id/item_1"
        android:checked="true"
        android:icon="@drawable/ic_1"
        android:title="@string/title_1" />
</group>

<group android:id="@+id/group2" android:checkableBehavior="single" >
    <item
        android:id="@+id/item_2"
        android:icon="@drawable/ic_2"
        android:title="@string/title_2" />
</group>

This will surely add dividers to your menus.

EDIT As you are adding menus programmatically, you should try to get access to each NavigationMenuView and add a decorator to them.

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
NavigationMenuView navMenuView = (NavigationMenuView) navigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(appContext,DividerItemDecoration.VERTICAL_LIST))

And here is the DividerItemDecoration class for you,

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;

    private int mOrientation;

    public DividerItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}
Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
1

try this

<View
android:layout_below="@+id/id_of_item_below_which_you_want_it"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"/>
AwaisMajeed
  • 2,254
  • 2
  • 10
  • 25