68

I am trying to get the navigation drawer to open below the toolbar.

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
tools:context=".MainActivity">
<RelativeLayout
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content">
    <include layout="@layout/toolbar"
        android:id="@+id/toolbar"/>
    <FrameLayout
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background_color"/>
</RelativeLayout>
<ListView
    android:id="@+id/drawer"
    android:layout_width="260dp"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_marginTop="56dp"
    android:layout_gravity="start">
</ListView>
</android.support.v4.widget.DrawerLayout>

How do I reformat the xml so that the navigation bar opens below the toolbar?

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
safaiyeh
  • 1,655
  • 3
  • 16
  • 35
  • 1
    @tyczj if you look at the latest version of the Google Play store. The navigation drawer is below the toolbar. I would like to implement something similar to that. – safaiyeh Nov 18 '14 at 01:57
  • @tyczj How do I handle toolbar actions when the navigation bar is open – safaiyeh Nov 18 '14 at 02:04
  • you dont, if you have actions you want the user to do when the drawer is open then you should put them in the drawers layout – tyczj Nov 18 '14 at 02:07
  • Possible duplicate of [How do I make DrawerLayout to display below the Toolbar?](http://stackoverflow.com/questions/26464326/how-do-i-make-drawerlayout-to-display-below-the-toolbar) – Shirish Herwade Apr 14 '16 at 11:17

7 Answers7

124

You should move DrawerLayout as top parent and move Toolbar out of DrawerLayout content container. In short this looks like:

RelativeLayout
 ----Toolbar
 ----DrawerLayout
     ---ContentView
     ---DrawerList 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/top_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/background_color" />

        <ListView
            android:id="@+id/drawer"
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:layout_gravity="start"
            android:layout_marginTop="56dp" />

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

However, Material Design guidelines state that Navigation Drawer should be above the Toolbar.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
24

You should simply add

android:layout_marginTop="@dimen/abc_action_bar_default_height_material"

to your layout which you are using as drawer.

This will automatically adjust the navigation drawer below the toolbar and also supports different screen sizes.

Muhammad Qasim
  • 461
  • 4
  • 14
  • 9
    Unfortunately, `@dimen/abc_action_bar_default_height_material` is marked as private in the compatibility library, so this solution is unsafe to use as the resource name may disappear in a future release of the library. – Ted Hopp Apr 13 '16 at 17:24
  • 5
    It still creates a shadow on the toolbar when sliding out. Thats not a desired effect. Not the best way to do it. – Ali Kazi Sep 13 '16 at 05:12
19

You can add layout_marginTop like this,

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

but drawer will appear as a the top layer to tool bar.


Here is another Choppy way to add it below to toolbar!!!

might not be the best but it works!

end result will look like this

enter image description here

If you create a project as a Navigation Drawer project(Navigation Drawer Activity) it will give you four XML files at the creation in your layout folder

  • app_bar_main
  • content_main
  • navigatin_main
  • activity_main

    enter image description here

how these xmls are linked? mostly i see include tag is used

Your Activity is linked with activity_main

  • activity_main has the app_bar_main and navigation_view(drawer)
  • app_bar_main has the toolbar and content_main by default

now lets remove activity_main and set its contents directly to app bar main and use it as the main layout for Activity.

To add the drawer under tool bar add it under the android.support.design.widget.AppBarLayout because is contains the toolbar and its should be on top.

here is an example of app_bar_main.XML

      <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="none.navhead.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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



    //------ taken from activity_main
    // content main
    <include layout="@layout/content_main" />

    // you need this padding
    <android.support.v4.widget.DrawerLayout
        android:paddingTop="?attr/actionBarSize"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start">

        <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:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />

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


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

p.s you can set app_bar_main.XML to setContentView of your activity just play around there are plenty of ways ;)

Charuක
  • 12,953
  • 5
  • 50
  • 88
  • 7
    This one works for me also on API23, where NavigationView is designed to be drawn behind system status bar. However, it still had shadow drawn at top, as if it was still drawn behind status bar, so I had to add app:insetForeground="@null" to the NavigationView, and it's drawn correctly below action bar (or actually Toolbar). This way also the nicely animated arrow at top left is visible, not covered by the NavigationView. – Pointer Null Apr 24 '16 at 21:05
  • Also i need to add `android:layout_marginTop="?attr/actionBarSize"` to `DrawerLayout`, to make content and navigation visible properly. – Sucipto May 20 '16 at 06:07
  • 1
    when i use this then data in fragment is un-clickable. plz say how to solve – roshan posakya Jan 18 '17 at 07:37
  • @roshan posakya where did you place your fragment? – Charuක Jan 18 '17 at 07:56
  • @Charuka thanks to reply ,, but i solve this problem using different way.. – roshan posakya Jan 19 '17 at 03:33
  • @PointerNull your `app:insetForeground="@null"` saved my day . it will hide that shadow above drawer. – Tejas Pandya Dec 25 '18 at 09:38
9

this is my layouts and work perfect: activity_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- AppBarLayout should be here -->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>

    <!-- add app:layout_behavior="@string/appbar_scrolling_view_behavior" -->

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        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:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer" />
    </android.support.v4.widget.DrawerLayout>
</android.support.design.widget.CoordinatorLayout>

app_bar_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  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:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context=".activty.MainActivity">
<include layout="@layout/content_main"/>
</FrameLayout>

result: Bellow toolbar

enter image description here

Tord Larsen
  • 2,670
  • 8
  • 33
  • 76
smaznet
  • 196
  • 2
  • 8
3

If you are using custom toolbar then use the drawer layout in this way..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <!-- The toolbar -->
    <android.support.v7.widget.Toolbar  
        android:id="@+id/my_awesome_toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <!-- drawer view -->
        <LinearLayout
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="left|start">
            ....
        </LinearLayout>

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

</LinearLayout>

and if you are not using custom toolbar then you have to set margin top to the drawer layout..

android:layout_marginTop ="?android:attr/actionBarSize"
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
3
<?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="false"
    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:layout_marginTop="?attr/actionBarSize"
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Tarun Umath
  • 900
  • 10
  • 7
0

An Easy and Good solution is set fitsSystemWindows=false for

android.support.v4.widget.DrawerLayout

that has id as

android:id="@+id/drawer_layout"

And for navigationView set layout_marginTop as ?attr/actionBarSize that would get the actionbar size and set it as margin

Here is complete code of activity_main.xml that has both the changes listed above.

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69