set as image as header CollapsingToolbarLayout on Collapse
Asked Answered
B

3

6

I want to stick image as background of CollapsingToolbarLayout when collapsed. I am able to show image in expanded form on CollapsingToolbarLayout but when the collapsed image disappears, I want the effect of the following GIF image:

image

My structure is as follows:

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


            <ImageView
                android:id="@+id/prof_img_picBackground"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@mipmap/ic_launcher"
                ndroid:fitsSystemWindows="true"
            app:layout_collapseMode="pin"/>



        <android.support.v7.widget.Toolbar
            android:id="@+id/anim_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>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    style="@style/overscroll_remove"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

Any help would be greatly appreciated, thanks in advance.

Back answered 26/10, 2015 at 9:36 Comment(4)
Is your issue got resolved? I am also looking for this kind of implementation.Succoth
how did you implement that?Jens
@DevendraSingh, ok i am writing down my answer, from there you can can use it.Back
@VikramSingh No, i thought you have done this #39263276 . actually i post a question hereJens
B
7

First create your them with this link, Transparent toolbar and set it as theme of your activity

xml code is as follows,

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <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"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:fitsSystemWindows="true">

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

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

and in activity,

 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        collapsingToolbarLayout.setTitle(itemTitle);
Back answered 2/9, 2016 at 5:7 Comment(0)
J
3

Try removing the contentScrim attribute from CollapsingToolbarLayout.

Joanjoana answered 14/2, 2016 at 14:59 Comment(0)
F
0

You can also just simply set contentScrim to a transparent color.

E.g.

<com.google.android.material.appbar.CollapsingToolbarLayout
    android:id="@+id/collapsingToolbarLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:contentScrim="@color/ap_transparent" <-- this line
    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
Flood answered 11/12, 2019 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.