Skip to content Skip to sidebar Skip to footer

How To Put Navigation Drawer Below Toolbar?

Here my navigation drawer is above toolbar.I also added some xml code.Please help me. here is my activity.xml

Solution 1:

certainly android:layout_marginTop="?attr/actionBarSize" do the job in

<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"
    android:layout_marginTop="?attr/actionBarSize">

But the problem is drawerlayout is top of the toolbar. That is why the fading here. you can remove fading by

mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));

But on some devices it may look wired.

Solution

When working with Android studio. We can create NavigationDrawerActiviity There are 3 files named

activity_main.xml

app_bar_main.xml

nav_header_main.xml

content_main.xml

So we can skip app_bar_main.xml and we can remove the fading.

Step 1

Make the root view of the activity main as Vertical LinearLayout

<LinearLayout 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:orientation="vertical"android:fitsSystemWindows="true"tools:context="com.example.MainActivity">
</LinearLayout>

In activity_main.xml add DrawerLayout and include content_main.xml in DrawerLayout. and Add AppBarLayout above the DrawerLayout.

<LinearLayoutxmlns: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:orientation="vertical"tools:context="com.qproinnovations.schoolmanagement.activity.HomeActivity"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="?attr/colorPrimary"app:popupTheme="@style/AppTheme.PopupOverlay" ></android.support.v7.widget.Toolbar></android.support.design.widget.AppBarLayout><android.support.v4.widget.DrawerLayoutandroid:id="@+id/drawer_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"tools:openDrawer="start"><!-- drawer view --><includelayout="@layout/content_main" /><!-- drawer content --><android.support.design.widget.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="start"android:fitsSystemWindows="true"app:menu="@menu/activity_home_drawer" /></android.support.v4.widget.DrawerLayout></LinearLayout>

Step 2

add and replace setContentView() of NavigationDrawerActiviity to

setContentView(R.layout.activity_main);

Finally we have

enter image description here

Solution 2:

Add

android:layout_marginTop="?attr/actionBarSize"

to your layout which you are using as drawer.

Solution 3:

In your navigation drawer xml you should add android:layout_marginTop ="?android:attr/actionBarSize" to the container.

Solution 4:

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

<LinearLayoutxmlns: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.Toolbarandroid: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.DrawerLayoutxmlns: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 --><LinearLayoutandroid: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"

Solution 5:

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 activity_main.xml code that has both the changes listed above.

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayoutxmlns: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"><includelayout="@layout/app_bar_main"android:layout_width="match_parent"android:layout_height="match_parent" /><android.support.design.widget.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginTop="?attr/actionBarSize"android:layout_gravity="start"android:fitsSystemWindows="true"app:menu="@menu/activity_main_drawer" /><!--app:headerLayout="@layout/nav_header_main"--></android.support.v4.widget.DrawerLayout>

Post a Comment for "How To Put Navigation Drawer Below Toolbar?"