Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable the shadow around card view in android

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

I tried this that is 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" > 

Thanks in advance.

like image 888
N Sharma Avatar asked Nov 06 '14 09:11

N Sharma


People also ask

Which attribute adds shadow to CardView?

CardView uses elevation property on Lollipop for shadows and falls back to a custom emulated shadow implementation on older platforms. Due to expensive nature of rounded corner clipping, on platforms before Lollipop, CardView does not clip its children that intersect with rounded corners.


2 Answers

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

like image 60
Loyea Avatar answered Oct 05 '22 05:10

Loyea


Try to put the elevation in Xml.

app:cardElevation="0dp" 

OR

cardView.setCardElevation(0); 

And check you are using the latest CardView library.

like image 25
Ashwin S Ashok Avatar answered Oct 05 '22 05:10

Ashwin S Ashok