CardView background color always white

AndroidAndroid RecyclerviewAndroid Cardview

Android Problem Overview


I am using RecyclerView with GridLayoutManager and I 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 appreciate if someone could help me out with this issue.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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));

Solution 3 - Android

Kotlin for XML

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

code

cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray));

Solution 4 - Android

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"));

Solution 5 - Android

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));

Solution 6 - Android

app:cardBackgroundColor="#488747"

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

Solution 7 - Android

You can use

app:cardBackgroundColor="@color/red"

or

android:backgroundTint="@color/red"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIshaanView Question on Stackoverflow
Solution 1 - AndroidLeandro Borges FerreiraView Answer on Stackoverflow
Solution 2 - AndroidNongthonbam TonthoiView Answer on Stackoverflow
Solution 3 - AndroidMohammed RousulView Answer on Stackoverflow
Solution 4 - AndroidyoAlex5View Answer on Stackoverflow
Solution 5 - AndroidAlireza NooraliView Answer on Stackoverflow
Solution 6 - AndroidBlackBlind567View Answer on Stackoverflow
Solution 7 - AndroidRaniaView Answer on Stackoverflow