you786 you786 - 1 month ago 14
Android Question

Adding a fading scrim to a CollapsingToolbarLayout

How do you get a fading scrim on the CollapsingToolbar Layout's title?

I've successfully set the text size and color using the

app:expandedTitleTextAppearance
attribute, but can't figure out how to give the text a background.

I know it's possible because I can see that WhatsApp has done it in its Group Info screen.

Answer Source

The only way I have been able to do it is to place a view behind the toolbar like this:

<View
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize"
     android:background="@drawable/shape_scrim"
     android:layout_gravity="bottom"/>

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

With the shape_scrim.xml looks like this:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:angle="90"
    android:endColor="#00000000"
    android:startColor="#BB000000"/>
</shape>