17

I am using 'com.android.support:cardview-v7:23.4.0' and I see the shadow but is not a dark grey shadow, and I would like to get a darker elevation shadow, but I do not see any attribute to get it, any ideas?

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp"
        app:cardElevation="8dp"
        app:cardPreventCornerOverlap="false"
        app:cardCornerRadius="1dp"
        app:cardUseCompatPadding="false">
        <include layout="@layout/any_layout"/>
    </android.support.v7.widget.CardView>
MLavoie
  • 9,671
  • 41
  • 36
  • 56
Billyjoker
  • 729
  • 1
  • 10
  • 31

5 Answers5

45

I believe that the shadow intensity is defined by the underlying theme that the app uses. So for API level more than 21, you can override the intensity of shadows. In your styles.xml add these two extra lines of ambientShadowAlpha and spotShadowAlpha. Please note that these values range from 0 to 1, with 0 being completely transparent and 1 being completely opaque(almost black) :

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:fontFamily">@font/barlow_family</item>
    <item name="android:ambientShadowAlpha">1</item>
    <item name="android:spotShadowAlpha">1</item>

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:statusBarColor">@android:color/background_light</item>
</style>

5
   <androidx.cardview.widget.CardView app:cardCornerRadius="10dp"
        app:cardUseCompatPadding="true"
        android:elevation="10dp"
        android:outlineSpotShadowColor="@color/black"
        android:outlineAmbientShadowColor="@color/grey"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

 app:cardUseCompatPadding="true"

try this

1

You can't change cardView shadow color but to make it darker you can use app:cardElevation="15dp" like

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="10dp"
    app:cardElevation="15dp"
    > </android.support.v7.widget.CardView>
yatin deokar
  • 730
  • 11
  • 20
0

there is no way to change color of official CardView elevation, look at this Carbon

Behnam Eskandari
  • 1,011
  • 12
  • 27
0

Try using this solution. Wrap your cardview in a RelativeLayout and give a background to it.

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="#FFE7E2E4">
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true"/>
</RelativeLayout>