2

I am using CardView inside a RecyclerView. After reading a lot I ended up with following 'NOT WORKING' code only on Android 5.1.1. On Android Version prior to this one its working nice.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_height="match_parent"
    android:background="#11ffffff"
    android:orientation="vertical"
    android:paddingBottom="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#11ffffff">
        <android.support.v7.widget.CardView
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@color/primary_bg_light"
            card_view:cardCornerRadius="2dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/primary_bg_light">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:paddingBottom="15dp"
                        android:paddingLeft="15dp"
                        android:textColor="@color/background_floating_material_light"
                        android:textSize="24sp"
                        android:textStyle="bold" />

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

I did try setting elevation using code also but same result.

theJango
  • 1,100
  • 10
  • 22
  • sorry, I don't see where you set elevation in this code.. – Gennadii Saprykin Aug 02 '15 at 15:15
  • where is the attribute `card_view:cardElevation` ? – Blackbelt Aug 02 '15 at 15:16
  • also, the parent `RelativeLayout` for `CardView` looks redundant.. I think background #11ffffff is almost black and card shadow might be not visible when you set elevation – Gennadii Saprykin Aug 02 '15 at 15:18
  • @Blackbelt : I did try with elevation from xml and also from code , both not working on 5.1.1 when Card View is used in RecyclerView. – theJango Aug 03 '15 at 07:29
  • @GennadiiSaprykin : Yeah may be redundant, but I did this as it was a proposed solution for a similar problem on Stack Overflow. Even without background color its not working. – theJango Aug 03 '15 at 07:30

5 Answers5

11

To make it both compatible above or below api 21, 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>
ShinChven
  • 473
  • 5
  • 11
2

Just simply add this code to your Card View in xml file!

    card_view:cardUseCompatPadding="true"

You have to import card_view with Alt+Enter ! And code below will be import in top !.

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

You'll see elevation is working on android 5 or 5+ .

Hadi Note
  • 1,386
  • 17
  • 16
2

Remove below line if you used in AndroidManifest.xml

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

remove this line from <application ...> </application> in AndroidManifest.xml file

android:hardwareAccelerated="false"

change to

android:hardwareAccelerated="true"

for more details Hardware acceleration

Harikesh
  • 311
  • 4
  • 6
0

set in card view

<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_height="match_parent"
android:layout_margin="6dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp">
Sanjib Suna
  • 284
  • 12
  • 18