Setting Elevation in XML on AppCompat CardView on Android 5.0 Setting Elevation in XML on AppCompat CardView on Android 5.0 android android

Setting Elevation in XML on AppCompat CardView on Android 5.0


It looks like a margin/padding problem, try to set the cardUseCompatPadding attribute to true. E.g.:

<android.support.v7.widget.CardView     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:card_view="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_margin="6dp"    card_view:cardUseCompatPadding="true"    card_view:cardElevation="4dp"    card_view:cardCornerRadius="3dp">

Explanation from Android doc :

CardView adds additional padding to draw shadows on platforms before L.

This may cause Cards to have different sizes between L and before L. If you need to align CardView with other Views, you may need api version specific dimension resources to account for the changes. As an alternative, you can set cardUseCompatPadding flag to true and CardView will add the same padding values on platforms L and after.

Since setting cardUseCompatPadding flag to true adds unnecessary gaps in the UI, default value is false.


If you have this line

android:hardwareAccelerated="false"

in manifest application tag your shadows were not displayed. try to remove this line

or use

android:hardwareAccelerated="true"

This worked for me! I hope it works for you too :)


You have to use the cardElevation attribute.
Androidx libraries:

You can use the MaterialCard included in the official Material Components library:

implementation 'com.google.android.material:material:1.x.x'

And in your layout:

    <com.google.android.material.card.MaterialCardView        app:cardElevation="xxdp"        app:cardUseCompatPadding="true"        ..>

Or the CardView in the androidx packages:

implementation 'androidx.cardview:cardview:1.x.x'

And in your layout:

     <androidx.cardview.widget.CardView        app:cardElevation="xxdp"        app:cardUseCompatPadding="true"        ..>

OLD support library:

<android.support.v7.widget.CardView    app:cardElevation="xxdp"     app:cardUseCompatPadding="true"    ..>