32

Am using the cardview in my android app. However the shadow is not showing. Here is the xml layout

The default optionsmenu shadow also not showing.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ECEDF0"
    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"
        android:layout_gravity="center"
        android:clipChildren="false"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="14dp"
        card_view:cardUseCompatPadding="true" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20dp"
            android:text="Google Play" />
    </android.support.v7.widget.CardView>

</LinearLayout>

Refer the attachment

Ram Patra
  • 16,266
  • 13
  • 66
  • 81
Dinesh T A
  • 2,087
  • 4
  • 26
  • 34

6 Answers6

91

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="2sp"
    card_view:cardUseCompatPadding="true"
    >
...
</android.support.v7.widget.CardView>
Ram Patra
  • 16,266
  • 13
  • 66
  • 81
  • can you please help me to solve this https://stackoverflow.com/questions/50066346/cardview-gridlayout-with-recyclerview-is-not-fit-for-all-screen-sizes-in-andro – Nikson Apr 27 '18 at 16:14
16

As mentioned there CardView not showing Shadow in Android L make sure that you are drawing your view using hardwareAccelerated = true

hardwareAccelerated = true enter image description here

hardwareAccelerated = false hardwareAccelerated CardView

See Android Hardware Acceleration for details

Community
  • 1
  • 1
AndreyICE
  • 3,574
  • 29
  • 27
5

For Lollipop and higher you should add some margins to the card:

<android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            />

Since the shadow lies outside of the actual view

deviant
  • 3,539
  • 4
  • 32
  • 47
1

for someone ,

one other thing that you should be aware of, shadows will not show if you have this line in the manifest:

android:hardwareAccelerated="false"

I tried all of the suggested stuff but it only worked for me when i removed the line, the reason i had the line was because my app works with a number of bitmap images and they were causing the app to crash.

Adnan Abdollah Zaki
  • 4,328
  • 6
  • 52
  • 58
  • 1
    Thank you @adnan, I was did a lot of search and was not able to make elevation or shadow on cardview but just remove the android:hardwareAccelerated="false" from manifest.xml and now it is looking as i required. – Zafar Imam Dec 28 '17 at 06:51
0

Please try to put android:hardwareAccelerated="false" androidManifest file may solve your issue because i also faced same issue and solved by adding 1 line only in manifest.

Madhav_nimavat
  • 401
  • 5
  • 19
0

Hey friends I got the solution of above problem.Just add this code in your xml.

<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_margin="8dp"
    android:id="@+id/griditem"
    android:layout_height="match_parent"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="3dp">`

I hope it will helpful for you...

sachin pangare
  • 1,527
  • 15
  • 11