How to implement shadows and elevation? Android Studio

972 Views Asked by At

I've been searching for a couple of days how to make these color shadows and the elevation effect but it's not clear anywhere. What's the easiest way to do this?

enter image description here

enter image description here

2

There are 2 best solutions below

0
AlexandrBikov On

Starting API 28 (Pie) View#setOutlineAmbientShadowColor(int color) and View#setOutlineSpotShadowColor(int color) are available in the View class.

If you use elevation on your View, you can use both methods to change the color of the shadow.

And you can use CardView to make shadows.

0
Pejman Azad On

one of the simplest ways:

CardView

card view well as support elevation.

   <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardBackgroundColor="@android:color/white"
        app:cardElevation="20dp" />

Note: In the background of the card view only you can use simple color. if you want more options you can put your image or drawable in child background.

Like this:

   <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardElevation="20dp" >

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/flag_peru"  <= use this options
>
.
.


</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>