1

There is no default shadow for collapsing toolbar expanded state. For my toolbars I usually add a View element below toolbar in xml to simulate shadow. Can i use the same technique for collapsing toolbar ? If so , how?

edit: I am still looking for an answer. How can I add a shadow below a collapsing toolbar?

  • Why not just use elevation? – Daniel Nugent Jun 25 '16 at 02:45
  • From what i know elevation doesnt support below api 21 –  Jun 25 '16 at 02:46
  • Am i wrong ? Should i use elevation ? –  Jun 25 '16 at 02:48
  • You are correct, but users on KitKat and lower don't really expect it. It's a bummer that the appcompat library doesn't take care of this. Have you seen this? http://stackoverflow.com/questions/26743325/appcompat-v21-toolbar-elevation-pre-lollipop – Daniel Nugent Jun 25 '16 at 03:24
  • If i use elevation , what happens for api below 21? will it just ignore the shadow and keep going or will the app crash? –  Jun 25 '16 at 15:24
  • also under which item should I add the elevation property? collapsingtoolbar/ appbar layout / toolbar ? –  Jun 25 '16 at 15:25
  • No, it won't crash, it just won't show a shadow. – Daniel Nugent Jun 25 '16 at 16:53

2 Answers2

1

I found an answer to this.

<android.support.design.widget.CoordinatorLayout
.... >
<android.support.design.widget.AppBarLayout
.... >
       <android.support.design.widget.CollapsingToolbarLayout
           ....
           android:id="@+id/yourcollapsingtoolbarID">

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

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap content"
    app:layout_anchor="@id/yourcollapsingtoolbarID"
    app:layout_anchorGravity="bottom">

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/toolbar_shadow"
        />
</RelativeLayout>

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

///////////toolbar_shadow.xml///////////

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
    android:startColor="@android:color/transparent"
    android:endColor="#88333333"
    android:angle="90"/>
</shape>
0

This one have shadow with adding views shadow effect.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout 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="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        android:theme="@style/MyTheme.AppBarOverlay"
        app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

MyStyle

<style name="MyTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
Laurence Pardz
  • 292
  • 3
  • 8