65

the CardView ( android.support.v7.cardview ) stays white even though I set a backround drawable via android:backround - The documentation gives me the feeling that it should work. No Idea what I am doing wrong here.

ligi
  • 39,001
  • 44
  • 144
  • 244
  • 1
    looks like it is not possible. Take a look here https://code.google.com/p/android/issues/detail?id=77843 – Blackbelt Dec 04 '14 at 16:51

13 Answers13

73

I know this is an old question, but I have a simple solution - just make the first child of your CardView an ImageView and specify the scale type to fitXY. You can get rid of the extra CardView padding by setting cardElevation and cardMaxElevation to 0dp:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:cardCornerRadius="4dp"
    app:cardElevation="0dp"
    app:cardMaxElevation="0dp">

    <ImageView
        android:src="@drawable/your_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>

    <... your layout

    .../>


</android.support.v7.widget.CardView>
Phil
  • 35,852
  • 23
  • 123
  • 164
19

for drawable or color just use:

cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);

for color use:

 cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);

but this one does not produce the intended results

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
14

Make Cardview will host one viewgroup for eg relative layout in this case and then simply set any background to relative layout.

<?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"
        android:id="@+id/list_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:id="@+id/list_container_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">

           <!--Add cardview contents-->

        </RelativeLayout>
    </android.support.v7.widget.CardView>
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
14

I got this working by adding a linearlayout in the cardview and then setting cardPreventCornerOverlap to false in the cardview.

   <android.support.v7.widget.CardView
    android:id="@+id/result_cv"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_margin="4dp"
    app:cardCornerRadius="16dp"
    app:cardElevation="8dp"
    app:cardPreventCornerOverlap="false"
    >

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient"
        android:gravity="center"
        >

       <!-- your views -->

    </LinearLayout>

    </android.support.v7.widget.CardView>
Pedram Tadayoni
  • 161
  • 1
  • 6
7

The command I used was:

cardView.setBackgroundResource(R.drawable.card_view_bg);

where card_view_bg is a custom XML resource file.

If you have some layouts inside the card view and these layouts are overlapping with the margins of the card view then you might need a separate custom background resource file for the layouts like the one I used for the card view background itself.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Ankit Deshmukh
  • 549
  • 5
  • 11
4

Try this

<?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"
    android:id="@+id/list_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardUseCompatPadding="true">

    <RelativeLayout
        android:id="@+id/list_container_bg"
        android:layout_width="match_parent"
        android:background="@drawable/yourbackground"
        android:layout_height="match_parent">

       <!--Add cardview contents-->

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

card_view:cardBackgroundColor="@android:color/holo_green_dark

here you can set any color or make it transparent by giving color transparent and if u wand some drawable like image or svg put RelativeLayout background

Virendra Varma
  • 895
  • 1
  • 12
  • 23
4

You can use LinearLayout or Relative layout inside CardView and set drawable background it like below :

<android.support.v7.widget.CardView
    android:id="@+id/card6"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    android:layout_marginTop="10dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/card4"
    app:cardCornerRadius="10dp"
    app:cardElevation="4dp"
    app:cardMaxElevation="8dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/card_bg"
        android:padding="10dp">

        <ImageView
            android:id="@+id/card6Image"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_launcher_round" />

        <TextView
            android:id="@+id/card6Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="5dp"
            android:text="Daily Check In"
            android:textColor="#ffffff"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/card6Description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="5dp"
            android:text="Just Check in Daily and Earn Credits"
            android:textColor="#ffffff"
            android:textSize="10sp" />
    </LinearLayout>

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

Below is my Drawable resource file :

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
<gradient
    android:angle="90"
    android:centerColor="#555994"
    android:endColor="#b5b6d2"
    android:startColor="#555994"
    android:type="linear" />

<corners
    android:radius="0dp"/>

chandan
  • 117
  • 8
4

you can set foreground as follow for the card view to set custom bg

android:foreground="@drawable/bg"

and here is the transparent bg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:width="1dp"
        android:color="#d9d9d9" />
    <corners android:radius="4dp" />
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
</shape>
3

You have 2 possible solutions.

  1. Setting background programmatically:
cardView.setBackgroundResource(R.drawable.background)
  1. Add some child to cardview that will fill cardview's bounds and set background on it:
<android.support.v7.widget.CardView
...>
     <ConstraintLayout
        android:background="@drawable/background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.v7.widget.CardView>

This way you can also give images some shape that you can configure by the shape of cardview. Disadvantage is that it is bad for performance.

0

if you want change background color on cardView just add this code on your cardview XML

app:cardBackgroundColor="@color/whatever_color_you"
Adhim Bagas
  • 131
  • 1
  • 3
0

Try this code:

cardView.setForeground(getResources().getDrawable(R.drawable.your_drawable));
Mohsents
  • 691
  • 11
  • 9
0
sCard.setBackgroundResource(R.drawable.scard_background);
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
-1

You can simply use the below line in your xml file.

app:cardBackgroundColor="@drawable/your_custom_theme_here"
Harshil Shah
  • 537
  • 4
  • 15