My card view elevation stopped working and showing shadow after I updated my android studio 3.3 and migrating my project to androidx artifacts.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="@dimen/card_margin"
app:cardCornerRadius="@dimen/card_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--Other Code-->
</LinearLayout>
</androidx.cardview.widget.CardView>
androidx.cardview.widget.CardView. A FrameLayout with a rounded corner background and shadow. CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms.
Android 7.0 introduces a new widget called CardView which essentially can be thought of as a FrameLayout with rounded corners and shadow based on its elevation. Note that a CardView wraps a layout and will often be the container used in a layout for each item within a ListView or RecyclerView.
Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.
If you want to use CardView padding on pre-L devices, and have it look the same on Lollipop+ devices, then you will need to use setUseCompatPadding(true) , or the XML variant cardUseCompatPadding="true" .
Add app:cardElevation="10dp"
property to cardview.
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="@dimen/card_margin"
**app:cardElevation="@dimen/card_margin"**
app:cardCornerRadius="@dimen/card_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--Other Code-->
</LinearLayout>
</androidx.cardview.widget.CardView>
you need to specify app:cardUseCompatPadding="true"
in your CardView
then set app:cardElevation="4dp"
with approbate value
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="12dp"
app:cardCornerRadius="12dp"
app:cardUseCompatPadding="true"
app:cardElevation="4dp">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With