Ethan Choi
Ethan Choi

Reputation: 2399

How to implement shadow (ambient light) background in android?

How to implement shadows like the image below?

I try many ways as CardView, elevation, many custom views, drawable. But that was different from the wanted shadows.

CardView

elevation of CardView is ambient light + key light. But, I want just ambient light elevation.

shadow of CardView.

I Want this shadow.

I want this shadow.

Upvotes: 7

Views: 4355

Answers (1)

Osagui Aghedo
Osagui Aghedo

Reputation: 350

Starting with API 28 you can set the color of the shadows.

If you want the ambient shadow only, then you need to set the color of the key light(aka spot light) to transparent.

You can do so by adding the following to your view tag in xml:

android:outlineAmbientShadowColor="@android:color/transparent"

Now only key/spot light will show.

and for ambient shadow

android:outlinesSpotShadowColor="@android:color/transparent"

Also check

setOutlineAmbientShadowColor() and setOutlinePostShadowColor()

to do it programmatically

You can also set them to other colors if you don't want a black shadow.

Upvotes: 3

Related Questions