Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CollapsingToolbarLayout with TabLayout
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (C) 2015 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
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"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabMode="scrollable"
app:tabContentStart="72dp" />
<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/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="parallax" />
</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.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="8dp"
android:src="@drawable/ic_done"
app:layout_anchor="@id/tabs"
app:layout_anchorGravity="center|left|start"
app:fabSize="mini"
app:layout_behavior="com.support.android.designlibdemo.ScrollAwareFABBehavior"
app:borderWidth="0dp" />
</android.support.design.widget.CoordinatorLayout>
@iPaulPro

This comment has been minimized.

Copy link
Owner Author

commented Jun 11, 2015

@iPaulPro

This comment has been minimized.

Copy link
Owner Author

commented Jun 11, 2015

Imgur

@jmrboosties

This comment has been minimized.

Copy link

commented Jun 17, 2015

Worked great for me, thanks man

@Sutirth

This comment has been minimized.

Copy link

commented Jun 18, 2015

Is it possible to add image over View Pager and once scrolled above the image gets hidden ???

@kyze8439690

This comment has been minimized.

Copy link

commented Jun 18, 2015

@Sutirth See the source code of CheeseDetailActivity.java in https://github.com/chrisbanes/cheesesquare, it has a similar effect.

@supriya18

This comment has been minimized.

Copy link

commented Jun 25, 2015

This is not working for me as i give gravity bottom to tab layout it disappers from the screen. what can be the solution???

@digitaltoad

This comment has been minimized.

Copy link

commented Jun 26, 2015

For me, this code works fine on api 22, but renders improperly on anything below that. The TabLayout is either invisible or rendered offscreen.

@davideas

This comment has been minimized.

Copy link

commented Jul 8, 2015

This is bugged, indeed it works only on devices with API 22. Let's wait.

@kundansviet

This comment has been minimized.

Copy link

commented Jul 24, 2015

hey i cant see tablayout after using this please help

@pranavlathigara

This comment has been minimized.

Copy link

commented Jul 30, 2015

Hii..
I cant able to make collapses toolbar
I used same code in my app that you mentioned
I have Listview Fragment under view pager

@sachins

This comment has been minimized.

Copy link

commented Aug 17, 2015

@kyze8439690 Is it possible to have fixed Tabs in the CheeseDetailActivity.java? I have been trying to get it to work with TabLayout, but it always shows weird Tab behavior. Following is my modified activity_detail.xml:

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

    <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.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        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:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <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.Light" />

            <android.support.design.widget.TabLayout
                android:id="@+id/detail_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:translationY="450dp"
                app:tabGravity="center" />

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

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


    <!--<include layout="@layout/nested_scrollview"/>-->


    <!--<android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_discuss"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>-->

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

Following the result of this:
device-2015-08-17-142820

I want the tabs to appear below the ImageView and not blend in like the way it does.

@sachins

This comment has been minimized.

Copy link

commented Aug 18, 2015

Edit: Please see @rufflez solution below which is not hacky.

I solved the above issue after many trial and error with below updated layout for activity_detail.xml file:

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

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

    <android.support.design.widget.TabLayout
        android:id="@+id/detail_tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        android:background="?attr/colorPrimary"
        app:layout_anchor="@+id/appbar"
        app:layout_anchorGravity="bottom"
        app:tabGravity="fill"
        app:tabMode="fixed" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_backdrop_height"
        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:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <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.Light" />
        </android.support.design.widget.CollapsingToolbarLayout>

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



    <!--<include layout="@layout/nested_scrollview"/>-->


    <!--<android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_discuss"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>-->

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

The changes I made above are:

  1. Move TabLayout out of CollapsingToolbarLayout and put it directly under CoordinatorLayout
  2. Set these attributes inside TabLayout:
  • android:layout_gravity="bottom" - To make sure AppBarLayout doesn't overlap on TabLayout
  • app:layout_anchor="@+id/appbar" and app:layout_anchorGravity="bottom" - To make sure tabs appear below AppBarLayout
  1. Also set android:layout_marginTop="?attr/actionBarSize" inside ViewPager to make sure TabLayout doesn't overlap any items in the list

The result:

Expanded

screenshot_expanded

Collapsed

screenshot_collapsed

@cseshaiban

This comment has been minimized.

Copy link

commented Aug 27, 2015

Why CollapsingToolbarLayout is not expanding to original size during second time scroll.
As it was at first launch.
Initially i got the result as was in code. and second time scrolling up its not behaves as per code.

Here is my code. Please help.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="?attr/colorPrimary"
app:contentScrim="?attr/colorPrimary"
android:fitsSystemWindows="true"
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:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:minHeight="?attr/actionBarSize"/>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabMode="scrollable"
android:background="?attr/colorPrimary"
app:contentScrim="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="2dp"
app:layout_collapseMode="pin" />
</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>

@pipipzz

This comment has been minimized.

Copy link

commented Aug 29, 2015

I did as suggested by @sachins for my screen which contains a header in the form of Relative Layout, which I want to collapse, a tab layout below that and a view pager attached to tab layout.

Here is my code for main activity http://paste.ubuntu.com/12222708/
Here is the code for my viewpager http://paste.ubuntu.com/12222718/

The contents of viewpagers are fragments that contain some layout covered in Scroll View so that it can be scrolled. The problem is, I am not able to collapse the header and scroll is working only below the tab layout and also the contents of viewpager are not being shown completely. Any help would be highly appreciated.

@adityanaique

This comment has been minimized.

Copy link

commented Sep 11, 2015

Thank you! :)

@chathudan

This comment has been minimized.

Copy link

commented Sep 24, 2015

@sachins could you please provide the working source code ?

@vitovalov

This comment has been minimized.

Copy link

commented Sep 30, 2015

@rufflez

This comment has been minimized.

Copy link

commented Oct 23, 2015

Here is my version:

<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=".activities.NewDogActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <ImageView
            android:id="@+id/background"
            android:layout_width="match_parent"
            android:layout_height="256dp"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"
            android:src="@drawable/butterfly"/>
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin"/>
    </android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        android:background="?attr/colorPrimary"
        app:tabMode="scrollable"/>
</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>

Looks like this:
new_dog_activity1
new_dog_activity2
new_dog_activity3

@ohmp

This comment has been minimized.

Copy link

commented Nov 4, 2015

not working while putting this code in fragment... need to have status bar with image background and when tab reached to top status bar color change according to theme color. this code working fine in activity but not working fragment please help.

@Xerosigma

This comment has been minimized.

Copy link

commented Nov 10, 2015

These solutions saved me a hell of a lot of time. Thanks all!

@Charlezz

This comment has been minimized.

Copy link

commented Nov 12, 2015

진짜 고마워요 많은 도움되었습니다.
Thanks a million guys!! these solutions are awesome

@tiembo

This comment has been minimized.

Copy link

commented Nov 12, 2015

Thanks @sachins! Your code did the trick to avoid drawing the tabs over the toolbar.

@milaptank

This comment has been minimized.

Copy link

commented Feb 5, 2016

Hey @sachin thanks your code helps me
does any one know how to tablayout title link this
04 profile

@bzkdjc

This comment has been minimized.

Copy link

commented Feb 20, 2016

@rufflez Thank you very much! Actually you solved that issue. Wow! Just spent 6 hours trying to solve it....

@JovieBrett

This comment has been minimized.

Copy link

commented Mar 6, 2016

@sachins do you know how can I fix this toolbar that is not including TabLayout's height?

EDIT: Nevermind, got fixed with @rufflez' code, thanks man!

2016-03-06

@gvsharma

This comment has been minimized.

Copy link

commented Mar 8, 2016

i want to inflate a layout after collapsingtoolbar scrolled..How?

@vasista2k

This comment has been minimized.

Copy link

commented Apr 15, 2016

where should i edit tab : text / colour ?

@JiajunWong

This comment has been minimized.

Copy link

commented May 12, 2016

@vitovalov. Incredible. Thanks

@beingentangled

This comment has been minimized.

Copy link

commented May 13, 2016

I'm not able to click inside the viewpager in the given format. there is a recycler view implemented inside one of the pages of viewpager. I'm not able to click on the card of the recycler view.

@MansoorJafari

This comment has been minimized.

Copy link

commented Jul 31, 2016

Hi
I'm using your code but app crash in api 16

android.view.InflateException: Binary XML file line #13: Error inflating class android.support.design.widget.TabLayout
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at yadbegir.jfa.mansoor.com.yadbegirApp.fragments.TabGroupFragment.onCreateView(TabGroupFragment.java:35)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 

@fanturbo

This comment has been minimized.

Copy link

commented Aug 1, 2016

In framgent of ViewPager,I use ListView rather than recyclerView and I find that when listview scroll toolbar won't scroll to hide.What's the difference between listview and recyclerview in this case and how can I achive this animation use listview? thanks!

@rathierry

This comment has been minimized.

Copy link

commented Aug 18, 2016

Hi @iPaulPro !!!
Where are the files "R.anim.fab_out" and "R.anim.fab_in" in ScrollAwareFABBehavior?
thanks

@svprakash001

This comment has been minimized.

Copy link

commented Nov 26, 2016

Cani use the AppBarLayout and Collapsing Toolbar without Cordinator Layout.Are the three needed to be used simultaneously to produce the result ?

@SanikaKadam

This comment has been minimized.

Copy link

commented Jun 6, 2017

If recyclerView have list empty tablayout and toolbar should not collpased ..Is this functionality can be obtained from your control.

@suresh777

This comment has been minimized.

Copy link

commented Jun 10, 2017

i am using cheesesquare code,in fragments i am doing some manipulation,if collapsing toolbar is there i am putting pin property to toolbar,when tablayout is there i am removing collapsingmode

after collapsing toolbar and i moving to another fragment there i am using tablayout so that i am pinning toolbar but it is overlapping tablayout

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/collapsing_toolbar"
        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
        android:fitsSystemWindows="true"

        >
        <View
            android:id="@+id/customView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_collapseMode="parallax"
            ></View>
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:paddingTop="?attr/actionBarSize"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            />

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



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

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >


</FrameLayout>


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

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

private void setToolbarPin(Toolbar toolbar)
{
ViewGroup.LayoutParams params = toolbar.getLayoutParams();
CollapsingToolbarLayout.LayoutParams newParams;
if (params instanceof CollapsingToolbarLayout.LayoutParams) {
newParams = (CollapsingToolbarLayout.LayoutParams)params;
} else {
newParams = new CollapsingToolbarLayout.LayoutParams(params);
}

    newParams.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_PIN);
    toolbar.setLayoutParams(newParams);
    ((MainActivity)getActivity()).customView.setVisibility(View.VISIBLE);

    //toolbar.requestLayout();
}

private void setToolbarPin(Toolbar toolbar)
{

    ViewGroup.LayoutParams params = toolbar.getLayoutParams();
    CollapsingToolbarLayout.LayoutParams newParams;
    if (params instanceof CollapsingToolbarLayout.LayoutParams) {
        newParams = (CollapsingToolbarLayout.LayoutParams)params;
    } else {
        newParams = new CollapsingToolbarLayout.LayoutParams(params);
    }

    newParams.setCollapseMode(CollapsingToolbarLayout.LayoutParams.COLLAPSE_MODE_OFF);
    toolbar.setLayoutParams(newParams);




  //  toolbar.requestLayout();

}

device-2017-06-09-233530
device-2017-06-09-233549
device-2017-06-09-233611
device-2017-06-09-233619

@knightcube

This comment has been minimized.

Copy link

commented Jul 26, 2017

@sachins your solution worked like cheese :)

@ItsRajesh4uguys

This comment has been minimized.

Copy link

commented Sep 12, 2017

Hello,

How can I use some Horizontal layout instead of Viewpager? I need the layout behavior as same as ViewPager. is that possible? Please guide me?

Thanks,
K. Rajesh

@mohan9

This comment has been minimized.

Copy link

commented Sep 22, 2017

thanks @rufflez

@chnouman

This comment has been minimized.

Copy link

commented Oct 11, 2017

Hy Everyone i have found on stackoverflow but don't get any helping information my problem is i have this collapsing appbar and tab view with viewpager in my third tab i have custom view in fragment which is scrollable. when i'm scrolling it up the collapsing is not working don't konw why but when i go to second tab and scrollup collapsing works correctly.

@manoranga

This comment has been minimized.

Copy link

commented Dec 7, 2017

great work.Though there are so many tutorials and sample codes are available in the the net those are not working properly.i searched many times every where ......but i didn't found a complete working app....

@muslimmuda15

This comment has been minimized.

Copy link

commented Mar 15, 2018

Great work, but in my case, I must change recycler view to text view if my data is empty
The horizontal scroll is okay, but I cannot vertically scroll.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.