Naddy
Naddy

Reputation: 2674

Android CardView doesn't work on Api 21

I am using android CardView and it works perfectly below Api 21. But when I use it on Api 21 i.e. Lollipop the xml attributes like cardElevation, cornerRadius doesn't work. What am I doing wrong?

This is my XML layout:

<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="wrap_content"
    android:orientation="vertical">
    <!-- A CardView that contains a TextView -->
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardElevation="2dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="4dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="6dp"
            android:paddingBottom="8dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:background="@color/white"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:layout_alignBottom="@+id/live_image">

            <TextView
                android:id="@+id/title_update"
                android:text="@string/msg_error"
                android:layout_width="match_parent"
                android:singleLine="true"
                android:ellipsize="end"
                android:textSize="18sp"
                android:background="@color/white"
                android:textColor="@color/black"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView_time"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/msg_error"
                    android:layout_weight="0.75"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black_translucent" />

                <TextView
                    android:id="@+id/textViewAlarm"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.25"
                    android:visibility="gone"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="@color/black"
                    android:textStyle="italic" />
            </LinearLayout>
        </LinearLayout>

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

Upvotes: 4

Views: 3358

Answers (2)

yyunikov
yyunikov

Reputation: 5907

I had exactly the same problem with CardView. Setting compatPadding to true fixed the issue.

In your layout try to add the following line to your CardView

card_view:cardUseCompatPadding="true"

Or if you are initializing in code, use

cardView.setUseCompatPadding(true);

Upvotes: 8

Nipper
Nipper

Reputation: 2380

I came across the same behaviour. I think the shadow is drawn outside the content. So you need space between the content of the view and it's border. I fixed it as follows:

Add margins to your CardView

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_margin="15dp"
    ... >

    ...

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

Upvotes: 0

Related Questions