redGREENblue
redGREENblue

Reputation: 3126

Toolbar elevation/shadow (for pre lollipop) when using CollapsingToolbarLayout

Below is the code I am using to have a collapsing toolbar. It works. But at the same time I am trying to have toolbar elevation which is not working in pre lollipop devices, so I am trying to do that by applying a view with a shadow drawable. It works, but in this case, has an unintended effect as the shadow is visible all the time (see below)

img 1

I want the shadow to be visible only when only the toolbar is visible (img 2

Any advise?

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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/appbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="72dp"
            app:expandedTitleTextAppearance="@style/ExpToolbarTitleStyle"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/transparent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                app:layout_scrollFlags="scroll|enterAlways">

                <ImageView
                    android:id="@+id/backdropimg"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/img"/>

                <com.makeramen.roundedimageview.RoundedImageView
                    android:id="@+id/profPic"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="16dp"
                    android:layout_marginLeft="16dp"
                    android:scaleType="fitCenter"
                    android:src="@drawable/imgprf"

                    app:riv_border_color="@color/icons"
                    app:riv_border_width="2dip"
                    app:riv_corner_radius="30dip"
                    app:riv_mutate_background="true"
                    app:riv_oval="true"/>
            </RelativeLayout>


            <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>
    <View
        android:id="@+id/toolbar_shadow"
        style="@style/ElevationFix"
        app:layout_scrollFlags="exitUntilCollapsed"
        android:layout_width="match_parent"
        android:layout_height="6dp"/>

    <android.support.v4.view.ViewPager .........
.........
.........
</android.support.design.widget.CoordinatorLayout>

Upvotes: 0

Views: 2708

Answers (2)

Luis Pe
Luis Pe

Reputation: 71

This is a bug, you can find here is Assigend as Small-Priority to Chris Banes (Android Developer).

https://code.google.com/p/android/issues/detail?id=182083

Upvotes: 1

Sourabh Bans
Sourabh Bans

Reputation: 3134

I suggest you to add the custom shadow view as foreground in your fragment container. i did same thing in my code like:

<android.support.v4.widget.NestedScrollView
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        **android:foreground="@drawable/header_shadow"**>

You can do one more thing put

<item name="header_shadow" type="drawable">@null</item> 

attibute in value-21 style.xml and before api 21 add it into your styles.xml like this..

   <item name="header_shadow" type="drawable">@drawable/bottom_shadow</item>

Upvotes: 0

Related Questions