Remove elevation shadow without removing elevation itself
Asked Answered
S

2

68

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

<android.support.design.widget.AppBarLayout
        app:elevation="0dp">
Saideman answered 9/12, 2017 at 12:12 Comment(5)
Why do you want to keep the elevation without shadow?Myriad
@Myriad Because I have a nested scroll content that I need to scroll behind bar and a semi transparent image view headerSaideman
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.Myriad
@Myriad indeed but unfortunately my nested scroll content goes over the ImageView in the AppBarLayout in that caseSaideman
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.Myriad
D
149

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" />
Deoxidize answered 6/2, 2019 at 21:17 Comment(1)
Only for API >= v21Gaberdine
K
40

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

Khadijahkhai answered 16/8, 2018 at 20:9 Comment(1)
Works lika a charm, this should be the accepted answer.Brazilein

© 2022 - 2024 — McMap. All rights reserved.