Draw NavigationView header below status bar with NavigationView behind status bar

6.6k Views Asked by At

I have been trying to draw the NavigationView header below the status bar, but it stays behind the status bar.

I have already set the property android:fitsSystemWindows="true" for both my DrawerLayout as well as my NavigationView, but to no avail.

Here's the XML

<?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/activity_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_scrollFlags="scroll|enterAlways|snap">

            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tab"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabMode="scrollable">

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

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

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

    <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:background="@android:color/white"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header" />

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

This is how it looks currently

3

There are 3 best solutions below

1
On

I had the same issue, on your NavigationView set fitsSystemWindows to false, leave rest as is. From what I understood from API >= 21 NavigationView goes behind status bar

3
On

Did you try adding android:fitsSystemWindows="true" to the root of your header layout? This worked for me.

2
On

Did you try to setFitsSystemWindows to false on the header View?

If you did and it did not work you can set a padding top to your View manually by observing the onApplyWindowInsetsListener on the header View or on the NavigationView:

 ViewCompat.setOnApplyWindowInsetsListener(headerView) { view, insets ->
      view.setPadding(0, insets.systemWindowInsetTop, 0, 0)
      insets
 }