37

I am working with cardviews but the problem is my CardView is not showing any elevation or shadow. I have already tried some of the methods suggested in stackoverflow answers like I have tried using elevation and shadow property. I have also tried using card_view:cardUseCompatPadding="true", but without any success. This is my xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:card_view="http://schemas.android.com/tools"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    app:cardBackgroundColor="#FAFBFD"
    app:cardElevation="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/post_profile_image"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/ic_launcher"
                android:layout_marginLeft="12dp"
                android:layout_marginTop="12dp"/>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/post_profile_image"
                android:layout_marginLeft="12dp"
                android:orientation="vertical"
                android:layout_marginTop="16dp">

                <TextView
                    android:id="@+id/post_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#000000"/>

                <TextView
                    android:id="@+id/post_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:text="Time"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/grayColor"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true">

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:drawable/ic_delete"
                    android:id="@+id/post_delete_btn"
                    android:visibility="invisible"/>

            </LinearLayout>

        </RelativeLayout>

        <ImageView
            android:id="@+id/post_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="12dp"
            android:adjustViewBounds="true"
            android:background="#00ffffff"
            android:src="@drawable/add_btn"
            android:visibility="gone"/>

        <TextView
            android:id="@+id/post_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="12dp"
            android:text="Text about Posting...."
            android:textAppearance="?android:attr/textAppearanceMedium"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:layout_marginBottom="8dp">

            <ImageButton
                android:id="@+id/like_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="12dp"
                android:background="#00ffffff"
                android:src="@drawable/grey_like_bt"/>

            <TextView
                android:id="@+id/post_like_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginTop="8dp"
                android:text="0 Likes"
                android:textColor="#2196F3"/>

            <ImageButton
                android:id="@+id/comment_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="32dp"
                android:background="#00ffffff"
                android:src="@drawable/comment_btn_gray"/>

            <TextView
                android:id="@+id/post_comment_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:layout_marginTop="8dp"
                android:text="0 Comments"
                android:textColor="#2196F3"/>

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:drawable/ic_menu_edit"
                    android:visibility="invisible"
                    android:id="@+id/post_edit_btn"
                    android:layout_alignParentRight="true"/>

            </RelativeLayout>

        </LinearLayout>

    </LinearLayout>

</android.support.v7.widget.CardView>

Dont know why it is not showing any elevation and shadow

Nikesh Patel
  • 521
  • 1
  • 5
  • 13

18 Answers18

106

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

<application android:hardwareAccelerated="true" ...>

see for details https://developer.android.com/guide/topics/graphics/hardware-accel.html?hl=ru

Ara Hakobyan
  • 1,682
  • 1
  • 10
  • 21
54
<android.support.v7.widget.CardView android:id="@+id/item_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="2dp"
    app:cardUseCompatPadding="true"
    app:cardElevation="2dp"
    app:cardBackgroundColor="@color/md_white_1000"
    android:foreground="?android:attr/selectableItemBackground"
    android:clickable="true">

</android.support.v7.widget.CardView>

app:cardUseCompatPadding="true" is the main catch here

ZeroOne
  • 8,996
  • 4
  • 27
  • 45
10

If your manifest file has a android:hardwareAccelerated="false" line, delete it.

10

Lot of good answers, but sometimes just one doesn't work for all. here's the one that worked for me after a lot of looking around,

Add this line to your CardView

app:cardUseCompatPadding="true"

So your cardview looks like this

  <androidx.cardview.widget.CardView
      android:id="@+id/cardView"
      android:layout_width="match_parent"
      app:cardUseCompatPadding="true"/>

Hope that works! Happy coding!

sud007
  • 5,824
  • 4
  • 56
  • 63
8

use this inside cardView to show shadow:

app:cardElevation="4dp"
app:cardMaxElevation="4dp"
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
6

You forget two things.

  • First of all, check your project's AndroidManifest.xml file, in the Application tag you have to write

<application android:hardwareAccelerated="true" ...>

instead of

<application android:hardwareAccelerated="false" ...>

  1. Then check the parent tag of your layout file is there written like below line.

  2. if you wanna use app: xmlns. then write : xmlns:app="http://schemas.android.com/apk/res-auto"

    OR xmlns:card_view="http://schemas.android.com/apk/res-auto"

NOTE: You don't need to write both simultaneous. Because it gives you duplicate attribute error.

The above line is most important when you use the attributes of res-auto.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/white"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp" />

</RelativeLayout>
Prince Dholakiya
  • 3,255
  • 27
  • 43
5

You should have to try this:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        app:cardElevation="3dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>

        <!--Put your other fields between this-->
</android.support.v7.widget.CardView>
Shubham Sejpal
  • 3,556
  • 2
  • 14
  • 31
3

There are two things that helped me solve this problem:

  1. Setting this in the application tag of my manifest:

    android:hardwareAccelerated="false"
    
  2. File > Invalidate cache and restart

Dharman
  • 30,962
  • 25
  • 85
  • 135
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
2

Try this

    <android.support.v7.widget.CardView
            android:id="@+id/driverCurrentJobsCardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/dim_8"
            android:background="#ffffff"
            app:cardCornerRadius="4dp"
            card_view:cardElevation="4dp"/>

And make sure that you are using this dependency in gradle

compile 'com.android.support:cardview-v7:23.2.1'
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
AbhayBohra
  • 2,047
  • 24
  • 36
2

remove below line from AndroidManifest.xml

android:hardwareAccelerated="false"
Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
1

Add this line in CardView widget

app:cardElevation="5dp"

hope this will help!

Akp
  • 259
  • 1
  • 9
1
<android.support.design.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools"
    app:cardCornerRadius="@dimen/_5sdp"
    card_view:cardCornerRadius="3dp"
    android:layout_margin="@dimen/_3sdp"
    card_view:cardElevation="10dp"
    card_view:cardUseCompatPadding="true">
</android.support.design.card.MaterialCardView>

Use above code and don't forget to use android:hardwareAccelerated="true". It will work definitely.

Eduard
  • 3,395
  • 8
  • 37
  • 62
Peter
  • 587
  • 6
  • 16
1

If there is a parent for the Card View, try setting a background color for the parent layout like a color #EDEDED , anything other than white. This will also make a clear difference between card and the background.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EDEDED"
    >
 <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardone"        
        android:elevation="10dp"   
        android:layout_margin="10dp"
       >

android:background="#EDEDED" on the parent layout makes the difference.

And don't use android:elevation="10dp" , use app:cardElevation="10dp"

Reejesh
  • 1,745
  • 2
  • 6
  • 15
0

Try putting this in your rootview:

xmlns:card_view="http://schemas.android.com/apk/res-auto" 

and use this code for elevation:

card_view:cardElevation="10dp"
Haris ali
  • 773
  • 1
  • 13
  • 19
  • Well i pasted your xml code and the cardview is showing shadows here. try using ``invalidate cache and restart`` option in android studio maybe that could help ! – Haris ali Apr 14 '17 at 07:50
0
     <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardCornerRadius="4dp"
    app:cardBackgroundColor="#ffff8800">
     ......
     </android.support.v7.widget.CardView>

Remove the background color first, add the elevation and later add the background again. Worked for me, though I had added the background through the layout designer.

Gator-aid
  • 51
  • 4
0

Apparently, I was using Theme.AppCompat instead of Theme.MaterialComponents in as my AppTheme.

 <style name="Theme.Main" parent="Theme.MaterialComponents.NoActionBar">
</style>

In your layout, use MaterialCardView instead of CardView

<com.google.android.material.card.MaterialCardView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    app:cardElevation="10dp">
Andrew Chelix
  • 1,012
  • 11
  • 16
-1

Try adding android:outlineProvider="bounds" to the CardView layout.

Pnemonic
  • 1,685
  • 17
  • 17
-2
<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"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/padding_small"
    android:layout_marginBottom="@dimen/padding_small"
    app:cardCornerRadius="4dp"
    app:cardElevation="4dp" >

</android.support.v7.widget.CardView>
bhaskar kurzekar
  • 238
  • 2
  • 14