Android Navigation drawer problem with notch/display cutouts

2.8k views Asked by At

I'm developing an Android application with the Android Studio template "Navigation Drawer Activity" but if I open the navigation drawer on a phone with Notch/display Cutouts the bigger status bar cover the top of the Navigation Drawer header.

How could I fix this?

enter image description here

Here are the Activity layout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:background="@color/colorPrimaryDark">

    <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>

And the navigation drawer header layout:

<LinearLayout 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="@dimen/nav_header_height"
    android:background="@drawable/navbar_size_edit"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin_bar_bottom"
    android:paddingLeft="@dimen/activity_horizontal_margin_bar"
    android:paddingRight="@dimen/activity_horizontal_margin_bar"
    android:paddingTop="@dimen/activity_vertical_margin_bar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/nav_header_desc"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:paddingBottom="@dimen/activity_vertical_margin_bar_bottom"
        app:srcCompat="@mipmap/avatar_me_round" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/app_name"
        android:textStyle="bold"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_version" />

</LinearLayout>
2

There are 2 answers

1
adarsha nayak v On BEST ANSWER

Change the attribute of android:fitsSystemWindows="true" to android:fitsSystemWindows="false". This is what worked for me.

2
Paul Spiesberger On

To be more specific than Adarsha Nayak V, set android:fitsSystemWindows to false in your NavigationView:

<androidx.drawerlayout.widget.DrawerLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:openDrawer="start">

<!-- Other elements... -->

    <com.google.android.material.navigation.NavigationView
                    android:id="@+id/nav_view"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="start"
                    android:fitsSystemWindows="false"
                    app:headerLayout="@layout/navigation_drawer_header"
                    app:menu="@menu/navigation_drawer"/>
</androidx.drawerlayout.widget.DrawerLayout>