11

Is it possible to have a CoordinatorLayout / CollapsingToolbarLayout in the fragments shown in the main container of a DrawerLayout?

An answer to another question suggests that each fragment could have its own Toolbar. But this doesn't work well with the ActionBarDrawerToggle as it requires a Toolbar to link to the open/close drawer behaviour.

Has anybody achieved this, or do you have pointers about this? Thanks.

EDIT: I've been focusing some efforts in putting a single Toolbar in the DrawerLayout, meant to stay there all the time, but was not able to get it to scroll (on a Nexus5 API 22). In this question it is mentioned that the CoordinatorLayout needs to be the main view. So maybe inserting it in a DrawerLayout (as below) is not going to work.

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

    <!-- main content -->
    <android.support.design.widget.CoordinatorLayout ...>

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

            <android.support.design.widget.CollapsingToolbarLayout ...>

                <ImageView .../>

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

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

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

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

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

    <!-- navigation drawer -->
    <android.support.design.widget.NavigationView ...>

        <!-- drawer content -->
        <fragment .../>

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

</android.support.v4.widget.DrawerLayout>
Community
  • 1
  • 1
ticofab
  • 7,169
  • 11
  • 44
  • 81
  • Why you need a different toolbar as android allows the fragments to modify toolbar from inside their class. So i suggest you modify the toolbar inside the fragment instead of using different toolbar for each fragment. – EEJ Aug 06 '15 at 13:20
  • Interesting... this is what I am trying to do now. I'll let you know – d4c0d312 Sep 29 '15 at 14:51

3 Answers3

5

As a matter of fact, yes you can. I was looking for the same thing and the link from your question lead me to this from Google Blogpost

Anyhow, below are my layout files, as for java code I did not change anything and Fragment calling remains the same.

activity_main.xml (Main file)

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

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

app_bar_main.xml (NavigationBar layout)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="192dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/content_main_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v4.widget.NestedScrollView>

And now you have "DrawerLayout + Fragments + CollapsingToolbarLayout"

JustADev
  • 218
  • 1
  • 6
  • 19
  • This is a great suggestions. However, I'd love it if you could help me adapt it to my situation. I also need to use a ViewPager to work with TabLayout. I changed the CollapsedToolbar with a TabLayout and it's fine but I have no idea where to position the ViewPager so that it doesnt interfere with the FrameLayout. – iBobb Feb 23 '16 at 23:47
  • @iBobb can you give me your layout files (activity_main.xml, app_bar_main.xml, and content_main.xml) so I can try with your case – JustADev Feb 24 '16 at 06:24
  • Hey, thanks for your time, I'd been at this problem for weeks. You can find my xml files in my separate question here: http://stackoverflow.com/questions/35590799/avoid-viewpager-going-under-toolbar My ViewPager isnt visible there as for now it's in my host fragment's layout (it's loaded in the content_main_frame). However, that way it goes under the toolbar. When I tried putting it somewhere in those 3 layout files, it's just not visible at all. – iBobb Feb 24 '16 at 12:43
  • @iBobb Sorry I will check it this weekend I wasn't able to do so earlier – JustADev Feb 26 '16 at 06:19
  • 2
    Don't worry, I managed to find a solution for now, hopefully the best one, so it's all working now. I posted it as the answer to my question above. Thanks anyway! – iBobb Feb 26 '16 at 13:07
2

This layout works for me

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_gravity="left"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".BaseDrawerActivity"
    android:foregroundGravity="left">


<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context=".myapp">

    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

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


<!-- The drawer -->
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
    android:id="@+id/drawer"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    style="@style/BaseStyle_Dark" />

Initialise the activity as normal

public class BaseDrawerActivity extends AppCompatActivity implements MenuInterface {

private DrawerLayout mDrawerLayout;
private StickyListHeadersListView menuItemListView;
private static MenuItemAdapter menuItemAdapter;
private ActionBarDrawerToggle mDrawerToggle;

private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        menuItemListView = (StickyListHeadersListView) findViewById(R.id.drawer);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (savedInstanceState == null) {

        }

        mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.home_open,R.string.home_close);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();

    }

This will allow you to add the usual CoordinatorLayout features.

d4c0d312
  • 719
  • 7
  • 21
  • I finally managed to combine Navigation bar + CoordinatorLayout + Toolbar + Fragments + TabLayout + AppBarLayout thanks to this answer, android4devs NavigationDrawer tutorial and CodePath's NavigationDrawer tutorial. – Avamander Aug 23 '16 at 15:47
0

I had the same problem but I didn't need the ImageView, so here is my solution:

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    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:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <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.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="@dimen/toolbar_elevation"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_view"/>
</android.support.v4.widget.DrawerLayout>

Hope it helps!

Gnzlt
  • 3,142
  • 1
  • 15
  • 21