Search code examples
androidandroid-layoutdrawerlayout

Remove DrawerLayout top padding that appear in api version > 21


So, basically I am implementing drawerLayout with custom menu (I am using recyclerView instead of navigationView). Top padding is appearing if I run the app in latest version of android (> lollipop). Is there anyway to remove the top padding? I tried to set fitSystemWindows=true on drawerLayout but no avail.

Code

<android.support.v4.widget.DrawerLayout
        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_store_landing"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            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">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="@color/drawer_bg"
                    android:paddingLeft="20dp">
                    <ImageView
                        android:id="@+id/menuIcon"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:layout_centerVertical="true"
                        android:src="@drawable/location_menu"
                        android:layout_marginRight="10dp"/>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/menuIcon">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Jalan Tunku Ismail"
                            android:textColor="@android:color/white"
                            android:ellipsize="end"
                            android:singleLine="true"
                            android:textStyle="bold"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Tap to change location"
                            android:textColor="@color/grey_text" />
                    </LinearLayout>
                </RelativeLayout>


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="@color/drawer_bg"
                    android:paddingLeft="20dp">
                    <ImageView
                        android:id="@+id/menuIcon2"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:layout_centerVertical="true"
                        android:src="@drawable/store_menu"
                        android:layout_marginRight="10dp"/>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/menuIcon2">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Jaya Grocer, Kuala Lumpur"
                            android:textColor="@android:color/white"
                            android:ellipsize="end"
                            android:singleLine="true"
                            android:textStyle="bold"/>
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Tap to change store"
                            android:textColor="@color/grey_text" />
                    </LinearLayout>
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="@color/drawer_bg"
                    android:paddingLeft="20dp">
                    <ImageView
                        android:id="@+id/menuIcon3"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:src="@drawable/my_account"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="10dp"/>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/menuIcon3">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="My Account"
                            android:textColor="@android:color/white"
                            android:ellipsize="end"
                            android:singleLine="true"
                            android:textStyle="bold"/>
                    </LinearLayout>
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:background="@color/drawer_bg"
                    android:paddingLeft="20dp">
                    <ImageView
                        android:id="@+id/menuIcon4"
                        android:layout_width="35dp"
                        android:layout_height="35dp"
                        android:layout_centerVertical="true"
                        android:src="@drawable/my_shopping_list"
                        android:layout_marginRight="10dp"/>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/menuIcon4">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="My Shopping List"
                            android:textColor="@android:color/white"
                            android:ellipsize="end"
                            android:singleLine="true"
                            android:textStyle="bold"/>
                    </LinearLayout>
                </RelativeLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/grey_text" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/menuList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </android.support.v4.widget.DrawerLayout>

Screenshot:

Left - Lolliop, Right - Kitkat enter image description here

Fixed!

Remove android:fitsSystemWindows="true" in NestedScrollView resolves the problem.


Solution

  • OK, found the bug. The culprit is android:fitsSystemWindows="true" in NestedScrollView. Remove this line and it resolves my problem. I am wondering why it is not affecting older version of android (< Kitkat).