Android Toolbar overlapping TabLayout in CollapsingToolbarLayout
Y

6

7

I'm trying to make CollapsingToolbarLayout with Toolbar and TabLayout below it, but they overlapping each other and I get this

enter image description here

I've tried many solutions, but still have this problem. Here is my xml:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/colorAppPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:background="@drawable/material_plane"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/header_png"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

                <ImageView
                    android:id="@+id/imageViewPhoto"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_centerInParent="true" />

                <TextView
                    android:id="@+id/textViewName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="false"
                    android:layout_below="@+id/imageViewPhoto"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="TEXT"
                    android:textColor="@color/white"
                    android:textSize="16dp" />
            </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                android:gravity="top"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways">

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textColor="@color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                android:fitsSystemWindows="true"
                app:tabBackground="@android:color/transparent"
                app:tabMode="scrollable" />

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
Yaron answered 5/2, 2016 at 11:28 Comment(0)
S
5

Try to remove this from your RelativeLayout:

app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

Or just let them be like this:

app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

It was a bug i guess.


UPDATED: check this link: http://blog.grafixartist.com/parallax-scrolling-tabs-design-support-library/

And similar question: How to use a TabLayout with Toolbar inside CollapsingToolbarLayout?

<android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="@color/colorPrimaryDark"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="@drawable/header"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop">

            <ImageView
                android:id="@+id/imageViewPhoto"
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:layout_centerInParent="true"
                app:layout_collapseMode="parallax" />

            <TextView
                android:id="@+id/textViewName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="false"
                android:layout_below="@+id/imageViewPhoto"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:text="TEXT"
                android:textSize="16dp" />

        </RelativeLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:fitsSystemWindows="true"
            android:gravity="top"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="20dp"
                android:textStyle="bold" />
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/htab_tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            app:tabIndicatorColor="@android:color/white" />

    </android.support.design.widget.CollapsingToolbarLayout>
Signification answered 5/2, 2016 at 11:36 Comment(11)
I tried this, but then my CollapsingToolbarLayout become higher and I can't put TabLayout inside. (I need transparent TabLayout)Yaron
Do you want something like this example? : github.com/TheLittleNaruto/SupportDesignExample i just updated the answer.Dennet
So, the answer is correct, you should put that TabLayout under CollapsingToolbarLayout.and if you want it transparent, you can check this out: #32372891 or - android:background="@android:color/transparent" that's the another issue btw.Dennet
Ok. I have transparent TabLayout, but how can I put it higher (at the bottom of RelativeLayout)? I tried margin, but then I have overlapping again.Yaron
That's why i said you need to put it under the CollapsingToolbarLayout check this layout: github.com/TheLittleNaruto/SupportDesignExample/blob/master/app/…Dennet
Now my TabLayout is under RelativeLayout, but not insideYaron
So, what is the problem? did it collapsed perfectly?Dennet
Let me show you. Screenshot. Red arrow shows TabLayout position and green arrow shows position where I want to locate it.Yaron
Still got overlapping :(Yaron
That link should do what you are looking for, perhaps you missed something.Dennet
I set Toolbar height to (toolbar_height+tabllayout_height) and now it's look great! Thank you very much! :)Yaron
A
2

I also got into similar kind of problem, i tried removing android:fitsSystemWindows="true" from CoordinatorLayout. And it worked.

Adaurd answered 12/6, 2016 at 7:30 Comment(0)
G
2

I found a way to resolve the using just add this line in toolbar layout.

android:layout_marginBottom="48dp"
Glantz answered 6/7, 2018 at 8:26 Comment(0)
G
0

Edit your XML like this:

 <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:fitsSystemWindows="true"
            android:marginTop="?attr/actionBarSize"
            app:tabBackground="@android:color/transparent"
            app:tabMode="scrollable"/>

Comment if it does not work

Geodynamics answered 5/2, 2016 at 11:35 Comment(0)
A
0

You have to keep your TabLayout below the CollapsingToolbarLayout, and it will be working sure, you can see my code from below:

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="@dimen/_32sdp"
        app:expandedTitleMarginEnd="@dimen/_64sdp"
        app:expandedTitleMarginStart="@dimen/_48sdp"
        app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed">

        <!--  Your View that you want to hide on animation -->
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_250sdp"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <!-- Your toolbar should always below your View otherwise it won't be visible  -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
            app:title="title text"
            app:titleTextColor="@color/white" />

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

    <!--your tablayout should be here which will come below the toolbar-->
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_50sdp"
        android:layout_below="@+id/toolbar"
        android:layout_gravity="bottom"
        android:background="@color/theme_color"
        android:overScrollMode="never"
        android:scrollbars="horizontal"
        android:visibility="visible"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom"
        app:tabIndicatorColor="@color/orrange"
        app:tabMode="scrollable" />

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


<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

</android.support.v4.widget.NestedScrollView>

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

All the best.

Astragalus answered 28/11, 2017 at 6:4 Comment(0)
F
-1

Make sure you are using the following structure:

<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.TabLayout />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

In which you need to make sure that only toolbar has:

android:layout_marginBottom="48dp"
app:layout_collapseMode="pin"

<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"
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
      android:layout_gravity="top"
      android:layout_marginBottom="48dp"
      app:layout_collapseMode="pin"
      >

And CollapsingToolbarLayout Must contain

app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"

<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
app:titleEnabled="false">
Fragonard answered 18/3, 2018 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.