Marco Cutecchia Marco Cutecchia - 1 month ago 11
Android Question

CollapsingToolbarLayout contentScrim is ignored?

I'm using CollapsingToolbarLayout to show the cover image for an article when it's not collapsed, to do this I use setBackground in my Activity. It used to show the image only when not collapsed and then turn my app primary color after i scrolled a bit but now it just keep showing the image as the toolbar background. Here's my layout(which is basically just the android scrolling activity example layout): https://gist.github.com/anonymous/7c68a84e40568d276fd329370e5224fa

In my code I just call:

findViewById(R.id.viewarticle_appbarlayout).setBackground(image);

Answer

Not sure why you choose to set the image as a background and on the AppBarLayout. You are using CollapsingToolbarLayout so you can add an ImageView as a child then you can control it's collapse mode. Basically the background and contentScrim is controlled by the the CollapsingToolbarLayout.

<?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"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/viewarticle_appbarlayout"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/article_collapsingtoolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/article_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/temp_img"
            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" />

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