57

Is there a way for AppBarLayout to no drop shadow and keep its elevation?

<android.support.design.widget.AppBarLayout
        app:elevation="0dp">
Jocky Doe
  • 1,771
  • 2
  • 14
  • 25
  • Why do you want to keep the elevation without shadow? – azizbekian Dec 09 '17 at 12:15
  • @azizbekian Because I have a nested scroll content that I need to scroll behind bar and a semi transparent image view header – Jocky Doe Dec 09 '17 at 12:19
  • If you make all of those view's have the same elevation value, then they'll end up being on the one z ordering, thus no shadow would be shown. – azizbekian Dec 09 '17 at 12:20
  • @azizbekian indeed but unfortunately my nested scroll content goes over the `ImageView` in the `AppBarLayout` in that case – Jocky Doe Dec 09 '17 at 12:27
  • 1
    Restructure your layout in a way, that `AppBarLayout` is declared at the end of the `xml` file, thus it will be always drawn on top of `NestedScrollView`. – azizbekian Dec 09 '17 at 12:33

2 Answers2

107

To complete M.Sandholtz answer, you can also define this in XML, with outlineProvider="none".

<View
    android:id="@+id/viewElevationNoShadow"
    android:outlineProvider="none"
    android:elevation="4dp"/>
Sean Blahovici
  • 2,640
  • 2
  • 20
  • 23
36

I just ran into this same problem and this is what fixed it for me:

val withElevationNoShadow = view.findViewById<*your view type*>(*your view id*)
withElevationNoShadow.outlineProvider = null

Keep in mind that the code above is Kotlin, but the Java is almost identical.

This works because shadows are drawn by ViewOutlineProviders. By setting your view's ViewOutlineProvider to null, you take away the default shadow.

For more info about ViewOutlineProviders check out

https://developer.android.com/reference/android/view/ViewOutlineProvider

and

https://developer.android.com/training/material/shadows-clipping

Clark Sandholtz
  • 552
  • 4
  • 11