89

I'm using the Designs Support Libraries NavigationView like this:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <!-- put your main layout here -->
    <include layout="@layout/drawer_main_layout"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view"/>

</android.support.v4.widget.DrawerLayout>

And I have set this menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_dashboard"
            android:title="Home" />
        <item
            android:id="@+id/nav_messages"
            android:icon="@drawable/ic_event"
            android:title="Messages" />
        <item
            android:id="@+id/nav_friends"
            android:icon="@drawable/ic_headset"
            android:title="Friends" />
        <item
            android:id="@+id/nav_discussion"
            android:icon="@drawable/ic_forum"
            android:title="Discussion" />
    </group>

    <item android:title="Sub items">
        <menu>
            <item
                android:icon="@drawable/ic_dashboard"
                android:title="Sub item 1" />
            <item
                android:icon="@drawable/ic_forum"
                android:title="Sub item 2" />
        </menu>
    </item>

</menu>

Is there any way to use the NavigationView with a layout rather than a menu?

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Panayiotis Irakleous
  • 2,696
  • 1
  • 23
  • 36

2 Answers2

193

Here's how I solved it, and worked perfectly:

<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include layout="@layout/nav_header" />

        <ListView
            android:id="@+id/menuItemsListView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>

</android.support.design.widget.NavigationView>
Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Hamzeh Soboh
  • 7,572
  • 5
  • 43
  • 54
38

Yes You can .... As I have done ... Just take your custom layout inside the NavigationView

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.NavigationView
        android:id="@+id/navView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <ListView
            android:entries="@array/test"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

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

</android.support.v4.widget.DrawerLayout>

The above code work for me... But don't forget to remove app:menu from NavigationView . Otherwise it will overlap your custom view on the menu items.

Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
Moinkhan
  • 12,732
  • 5
  • 48
  • 65
  • 1
    The listview is appearing over the headerview. How to put it below headerview? – Sudheesh Mohan Jun 19 '15 at 09:39
  • If u are using `ListView` as a child view then you can not use `NavigationView's` Header. So If you are using `ListView` you can add headerView in `ListView`. – Moinkhan Jun 19 '15 at 09:42
  • 1
    When I added headerview in Listview, headerview disappeared. code – Sudheesh Mohan Jun 19 '15 at 09:45
  • Bro currently Design support library is buggy... You can do it. If you want to add headerview you have inflate it and add in listview programatically .. – Moinkhan Jun 19 '15 at 09:48
  • 7
    What the NavigationView gives more if then we use ListView inside of it with a custom row as we are doing so far until now? – Davideas Jun 24 '15 at 13:14
  • NavigationView is just for give a navigation in your app. You can also use it without drawer. It's just allow you to do a less code if you want simple label and image in your menu optional header that's it.... – Moinkhan Jul 23 '15 at 03:35
  • `LayoutInflater inflater = getLayoutInflater(); View listHeaderView = inflater.inflate(R.layout.header_list,null, false); mDrawerList.addHeaderView(listHeaderView);` – yalematta Jul 14 '17 at 09:33