Android elevation of View in CardView is not shown

Mokkapps picture Mokkapps · Dec 17, 2014 · Viewed 12.7k times · Source

I have a RelativeLayout inside a CardView that i want to elevate in Android Lollipop but the elevation is not shown (the shadow is not shown):

enter image description here

My layout file:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android.support.v7.cardview="http://schemas.android.com/apk/res-auto"
    style="@style/CardViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="5dp"
    android:orientation="horizontal"
    android.support.v7.cardview:cardElevation="5dp">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:paddingBottom="5dp">

    <ImageView
        android:id="@+id/article_thumb"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/app_name"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/article_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/article_thumb"
        android:layout_marginEnd="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginStart="10dp"
        android:ellipsize="middle"
        android:linksClickable="false"
        android:longClickable="false"
        android:text="@string/title"
        android:textAppearance="@style/TextAppearance.AppCompat.Title"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/article_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/article_title"
        android:layout_alignStart="@+id/article_title"
        android:layout_below="@+id/article_title"
        android:layout_marginTop="10dp"
        android:text="@string/date"
        android:textAppearance="@style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small"
        android:textColor="@color/gray" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/article_title"
        android:layout_alignRight="@+id/article_title"
        android:layout_below="@+id/article_title"
        android:layout_marginTop="10dp"
        android:elevation="30dp"
        android:translationZ="30dp">

        <ImageView
            android:id="@+id/redbubble"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center"
            android:contentDescription="@string/app_name"
            android:scaleType="centerCrop"
            android:src="@drawable/redbubble" />

        <TextView
            android:id="@+id/article_comment_bubble"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textColor="@color/white" />
    </RelativeLayout>

</RelativeLayout>

So inside this layout I added this two lines to the RelativeLayout to make the elevation:

android:elevation="30dp"
android:translationZ="30dp"

Anyone an idea why it is not shown? At the moment I test it with a Emulator using Android API 21 (Lollipop).

Answer

Rahul Ahuja picture Rahul Ahuja · May 22, 2015

In the root layout in the xml you need

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

below is the code for a cardview

card_view:cardElevation="5dp"