CardView not showing Shadow in Android L

162,390

Solution 1

After going through the docs again, I finally found the solution.

Just add card_view:cardUseCompatPadding="true" to your CardView and shadows will appear on Lollipop devices.

What happens is, the content area in a CardView take different sizes on pre-lollipop and lollipop devices. So in lollipop devices the shadow is actually covered by the card so its not visible. By adding this attribute the content area remains the same across all devices and the shadow becomes visible.

My xml code is like :

<android.support.v7.widget.CardView
    android:id="@+id/media_card_view"
    android:layout_width="match_parent"
    android:layout_height="130dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardElevation="2dp"
    card_view:cardUseCompatPadding="true"
    >
...
</android.support.v7.widget.CardView>

Solution 2

Do not forget that to draw shadow you must use hardwareAccelerated drawing

hardwareAccelerated = true

enter image description here

hardwareAccelerated = false

hardwareAccelerated CardView

See Android Hardware Acceleration for details

Solution 3

use app:cardUseCompatPadding="true" inside your cardview. For Example

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="@dimen/cardviewMarginRight"
        app:cardBackgroundColor="@color/menudetailsbgcolor"
        app:cardCornerRadius="@dimen/cardCornerRadius"
        app:cardUseCompatPadding="true"
        app:elevation="0dp">
    </android.support.v7.widget.CardView>

Solution 4

check hardwareAccelerated in manifest make it true , making it false removes shadows , when false shadow appears in xml preview but not in phone .

Solution 5

Add this line to CardView....

card_view:cardUseCompatPadding="true" //for enable shadow
card_view:cardElevation="9dp" // this for how much shadow you want to show

Tips

You can avoid layout_marginTop and layout_marginBottom as shadow itself takes some space to the up and down of it.The amount space defined by how much you will use in card_view:cardElevation="ndp" .

Happy Coding (:

Share:
162,390
isumit
Author by

isumit

Updated on July 08, 2022

Comments

  • isumit
    isumit almost 2 years

    My Cardview inside Listview is not showing shadow in Android L(Nexus 5). Also the round edges are not properly shown. Here is the code for Listview's Adapter View :

    <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/white"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp" >
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingTop="@dimen/activity_vertical_margin" >
    
            <TextView
                android:id="@+id/tvName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="@dimen/padding_small"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:textAppearance="?android:attr/textAppearanceLarge" />
    
            <ImageView
                android:id="@+id/ivPicture"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tvName"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter" />
    
            <TextView
                android:id="@+id/tvDetail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ivPicture"
                android:layout_centerHorizontal="true"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
    

    And the ListView xml :

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/app_bg" >
    
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:cacheColorHint="#00000000"
        android:divider="@android:color/transparent"
        android:drawSelectorOnTop="true"
        android:smoothScrollbar="true" />
    
    <ProgressBar
        android:id="@+id/progressBarMain"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" /></RelativeLayout>
    

    It works fine on pre-L devices with proper shadow and rounded corners. But not working aon Android L device. Can you tell what i am missing here?

  • isumit
    isumit over 9 years
    adding margins to cardview from layout file works. Didn't tried from code
  • Aksel Fatih
    Aksel Fatih almost 8 years
    Hardware acceleration is enabled by default if your Target API level is >=14 !
  • Ajith M A
    Ajith M A almost 8 years
    I already had the card_view:cardUseCompatPadding values set, still was not able to get the shaddows. What i was missing is this, the margins. And it really worked. Thanks.
  • Ram Patra
    Ram Patra over 7 years
    @alex-lockwood Can you please explain your change i.e, card_view:cardElevation="2sp" to card_view:cardElevation="2dp" ?
  • Alex Lockwood
    Alex Lockwood over 7 years
    @Ramswaroop sp should only be used for text sizes. dp should be used for all other dimensions.
  • Deepak Kumar
    Deepak Kumar over 7 years
    It's working,. But I don't understand reason behind this
  • behelit
    behelit about 7 years
    what's your margin value? is 8dp enough?
  • Remi
    Remi about 7 years
    I my case card_view:cardUseCompatPadding was needed. Additionally I need to change targetSdkVersion to 21. Changing margin has no effect.
  • fullmoon
    fullmoon almost 7 years
    thank you, the key was card_view:cardElevation and not android:cardElevation
  • Ram Patra
    Ram Patra almost 5 years
    @KishanSolanki124 I think the OP (@isumit) has lost track of this thread.
  • jhfdr3s
    jhfdr3s almost 3 years
    This sounds radical
  • Giddy Naya
    Giddy Naya over 2 years
    app:cardBackgroundColor="@android:color/white"... Most important. Add a background color!