Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CardView elevation not working with RecyclerView

CardView elevation works fine when tested in a separate application, but when the same code for the cardView is used to craft items of a RecyclerView, the elevation no longer appears.

Here is the code for the RecyclerView list item:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="wrap_content"
             android:layout_width="match_parent"
             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-auto"
             android:layout_margin="10dp">


<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    card_view:cardUseCompatPadding="true"
    app:cardMaxElevation="6dp"
    card_view:cardCornerRadius="3dp"
    card_view:cardElevation="24dp">

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

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/thumbnail"
            />
    </LinearLayout>


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

And here is my main activity xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                   xmlns:tools="http://schemas.android.com/tools"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="vertical"
                   android:background="@drawable/bitmap"
                   android:layerType="software"
                   android:padding="10dp"
    tools:context="com.example.android.vikramadityastimeline.MainActivity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/card_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    />



 </FrameLayout>
like image 452
Coolio Cena Avatar asked Nov 29 '22 22:11

Coolio Cena


2 Answers

To Support it, you need to specify app:cardUseCompatPadding="true" in your support CardView.

<android.support.v7.widget.CardView
        app:cardElevation="4dp"
        app:cardUseCompatPadding="true"
        app:cardMaxElevation="6dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</android.support.v7.widget.CardView>
like image 71
Harshad Pansuriya Avatar answered Dec 04 '22 13:12

Harshad Pansuriya


Use this in your card view. It solved my same problem.

app:cardUseCompatPadding="true"
like image 34
Tobibur Rahman Avatar answered Dec 04 '22 12:12

Tobibur Rahman