Andrew Quebe
Andrew Quebe

Reputation: 2293

Expand Navigation Drawer Over Action Bar/Toolbar

I've been materializing one of my apps and I added the Navigation Drawer that came out with Lollipop. Upon reading the docs, I found that I need to have the drawer overlay the action bar.

Searching Google led me to this post. The problem is, my drawer is set up differently and I'm wondering if I need to redo it or if I can just alter the work I've already done.

Code

ActivityDrawerLayout.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setBackgroundColor(Color.parseColor("#FFFFFF"));

        navigationDrawerItems = getResources().getStringArray(R.array.navigation_drawer_items);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        listView = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        //drawerLayout.setStatusBarBackgroundColor(Color.parseColor("#ACACAC"));
        // set up the drawer's list view with items and click listener
        listView.setAdapter(new NavDrawerAdapter());
        listView.setOnItemClickListener(new DrawerItemClickListener());

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        ...
    }

activity_nav_drawer.xml:

<LinearLayout
    android:id="@+id/main_parent_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:clipToPadding="false">

    <include layout="@layout/toolbar" />

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

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

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="304dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#FFFFFF"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />

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

Notice the fitsSystemWindows property is set.

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ToolbarCustomIconColor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true" />

Values-v21 Theme:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppThemeNavDrawer" parent="Theme.AppCompat.NoActionBar">
        <item name="colorAccent">#F8F8F8</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>
</resources>

And the final result of all of this is: Final result

As you can see it isn't expanding. Also, I didn't add the statusBarColor because my app allows the user to select the different colors so it would be useless.

Help is appreciated!

Upvotes: 0

Views: 1433

Answers (1)

user468311
user468311

Reputation:

I believe you need to modify your activity_nav_drawer.xml layout. I'd suggest following:

<android.support.v4.widget.DrawerLayout>
    <FrameLayout
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        <FrameLayout
            android:id="@+id/contentFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
        <include layout="@layout/toolbar" />
    </FrameLayout>
    <include
            android:id="@+id/leftDrawer"
            android:layout_gravity="start"
            layout="@layout/navigation" />
</android.support.v4.widget.DrawerLayout>

Upvotes: 1

Related Questions