107

I am working on demo application in which I am using the card view of the support library. By default, it is adding a shadow around it. I want to remove this shadow & should looks like simple.

I tried this (not working for me):

CardView cardView = (CardView) v.findViewById(R.id.cardView);
cardView.setElevation(0);

After doing these I am getting crash

11-06 15:12:17.018: E/AndroidRuntime(24315): FATAL EXCEPTION: main
11-06 15:12:17.018: E/AndroidRuntime(24315): Process: com.xyz, PID: 24315
11-06 15:12:17.018: E/AndroidRuntime(24315): java.lang.NoSuchMethodError: android.support.v7.widget.CardView.setElevation
11-06 15:12:17.018: E/AndroidRuntime(24315):    at com.xyz.adapters.RecycleViewAdapter.onCreateViewHolder(RecycleViewAdapter.java:85)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at com.xyz.adapters.RecycleViewAdapter.onCreateViewHolder(RecycleViewAdapter.java:1)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:2915)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:2511)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.LinearLayoutManager$RenderState.next(LinearLayoutManager.java:1425)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.LinearLayoutManager$RenderState.next(LinearLayoutManager.java:1425)

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res/com.xyz"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:orientation="horizontal" >
starball
  • 20,030
  • 7
  • 43
  • 238
N Sharma
  • 33,489
  • 95
  • 256
  • 444

13 Answers13

166

use this attribute in XML

card_view:cardElevation="0dp"

and remember add xmlns:card_view="http://schemas.android.com/tools" in your root layout.

OR you can call cardView.setCardElevation(0) to disable shadow programmatically.

cardView.setElevation() method and CardView attribute android:elevation will throw java.lang.NoSuchMethodError in platform before Android 5.0

Loyea
  • 3,359
  • 1
  • 18
  • 19
94

Try to put the elevation in Xml.

app:cardElevation="0dp"

OR

cardView.setCardElevation(0);

And check you are using the latest CardView library.

Ashwin S Ashok
  • 3,623
  • 2
  • 29
  • 36
  • These properties are available in latest library but when I use latest then my app crash with this http://stackoverflow.com/questions/26675682/exception-android-view-inflateexception-binary-xml-file-line-2-error-inflat – N Sharma Nov 06 '14 at 10:03
53

use app:cardElevation="0dp", don't use app:elevation="0dp"

Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
20

Just put this line inside your CardView:

app:cardElevation="0dp"

Hope it will help you.

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
Brahem Mohamed
  • 342
  • 2
  • 5
9

You can have it in XML like:

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    app:cardElevation="0dp"
    app:cardCornerRadius="0.5dp"
    app:cardPreventCornerOverlap="false"
    app:cardUseCompatPadding="true"
    >
   </android.support.v7.widget.CardView>

hope it helps you !!!

Hetal1311
  • 364
  • 3
  • 14
  • 1
    +1 - app:cardPreventCornerOverlap="false" - is required when it has rounded corners and cardElevation was set to 0dp. – Tarlan Ahad Mar 28 '21 at 14:48
5

try like this may help you,

CardView cardView = (CardView) v.findViewById(R.id.cardView);
cardView.setCardElevation(0);
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
  • There is no such method `setCardElevation` – N Sharma Nov 06 '14 at 09:51
  • This method is available in latest library but when I use new library then I am getting this http://stackoverflow.com/questions/26675682/exception-android-view-inflateexception-binary-xml-file-line-2-error-inflat – N Sharma Nov 06 '14 at 10:03
5

You should first add this to the your parent layout

xmlns:card_view="http://schemas.android.com/tools"

then set the elevation like this

card_view:cardElevation="0dp"

Udo
  • 1,129
  • 3
  • 15
  • 30
5
 <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardElevation="0dp"
        app:cardCornerRadius="2dp">
....`
 </android.support.v7.widget.CardView>
Bhavu
  • 51
  • 1
  • 2
  • 1
    Add some explanation with answer for how this answer help OP in fixing current issue – ρяσѕρєя K Oct 21 '16 at 05:13
  • 1
    Please don't make a duplicate answers. How same answer can be for multiple questions, even you didn't change a bit. Even if you find similar question then instead of replicating your answer you should mention a link below question. Duplicate [here](http://stackoverflow.com/questions/36624954/android-4-4-remove-shadow-from-cardview/40168570#40168570) – Rohan Khude Oct 21 '16 at 05:16
4

If anyone is looking for kotlin answer then this worked for me

card_layout.cardElevation = 0F
Syn3sthete
  • 4,151
  • 3
  • 23
  • 41
3

You have to use the following attributes

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="0dp"
    app:cardCornerRadius="0.5dp"
    app:cardPreventCornerOverlap="false"
    >
   </android.support.v7.widget.CardView>
Angel Shah
  • 41
  • 2
3

In my case, only setting of background alpha with suggested elevation and backgroundColor hide shadow border:

 this.setCardElevation(0);
 this.setCardBackgroundColor(getContext().getResources().getColor(android.R.color.transparent));
 this.getBackground().setAlpha(0);
Fragment
  • 1,555
  • 1
  • 26
  • 33
1

Attenion: Attribute outlineSpotShadowColor is only used in API level 28 and higher

app:cardElevation="0dp"
android:outlineSpotShadowColor="@android:color/transparent"
Mori
  • 2,653
  • 18
  • 24
0

CardView sets its own elevation during initialization, which will override whatever you set from XML. You should file this as a bug at chek this

@Override
public void initialize(CardViewDelegate cardView, Context context, int backgroundColor,
        float radius) {
    cardView.setBackgroundDrawable(new RoundRectDrawable(backgroundColor, radius));
    View view = (View) cardView;
    view.setClipToOutline(true);
    view.setElevation(context.getResources().getDimension(R.dimen.cardview_elevation));
}
Shyam
  • 6,376
  • 1
  • 24
  • 38