191

I use RecyclerView with GridLayoutManager and have each item as CardView.

Unfortunately, the CardView here does not seem to change its background color. I tried in layout and programmatically as well but I have tried nothing seems to work.

I have been struggling for quite a while. I would appreciate it if someone could help me out with this issue.

Aldan
  • 674
  • 9
  • 23
Ishaan
  • 3,658
  • 2
  • 23
  • 39

9 Answers9

450

If you want to change the card background color, use:

app:cardBackgroundColor="@somecolor"

like this:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/white">

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

Edit: As pointed by @imposible, you need to include

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

in your root XML tag in order to make this snippet function

Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39
Leandro Borges Ferreira
  • 12,422
  • 10
  • 53
  • 73
48

You can do it either in XML or programmatically:

In XML:

card_view:cardBackgroundColor="@android:color/red"

Programmatically:

cardView.setBackgroundColor(ContextCompat.getColor(this, R.color.my_color));
beigirad
  • 4,986
  • 2
  • 29
  • 52
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • 1
    Thank you for your answer, I solved it. I had changed the color programmatically somewhere which I couldn't spot. – Ishaan Jan 10 '17 at 06:49
14

Kotlin for XML

app:cardBackgroundColor="@android:color/red"

Programmatically:

cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray));
emen
  • 6,050
  • 11
  • 57
  • 94
Mohammed Rousul
  • 199
  • 3
  • 7
6

Android CardView background color

XML code

<android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="5dp"
        app:contentPadding="25dp"
        app:cardBackgroundColor="#e4bfef"
        app:cardElevation="4dp"
        app:cardMaxElevation="6dp" />

From the code

CardView card = findViewById(R.id.card_view_top);
card.setCardBackgroundColor(Color.parseColor("#E6E6E6"));
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
3

In XML:

app:cardBackgroundColor="@color/your_color_name"

Both in Java and Kotlin you can do it programmatically:

cardView.setCardBackgroundColor(ContextCompat.getColor(this, R.color.your_color_name));
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
0

If still not visible in design view, you can temporarily remove these lines

app:cardUseCompatPadding="true"

or

card_view:cardUseCompatPadding="true"

If you need them, you can then add them back at build time

DPRK
  • 1
  • 1
0

If someone still gets the white color instead of the color he picked, just change the emulator you working on, that's works for me

jenos kon
  • 504
  • 4
  • 7
-2

app:cardBackgroundColor="#488747"

use this in your card view and you can change a color of your card view

Vishnu Haridas
  • 7,355
  • 3
  • 28
  • 43
-2

You can use

app:cardBackgroundColor="@color/red"

or

android:backgroundTint="@color/red"
Thomas Dondorf
  • 23,416
  • 6
  • 84
  • 105
Rania
  • 1
  • 2