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.
This question is related to
android
android-recyclerview
android-cardview
You can do it either in XML or programmatically:
In XML:
card_view:cardBackgroundColor="@android:color/red"
Programmatically:
cardView.setCardBackgroundColor(Color.RED);
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"));
You can use
app:cardBackgroundColor="@color/red"
or
android:backgroundTint="@color/red"
app:cardBackgroundColor="#488747"
use this in your card view and you can change a color of your card view
Kotlin for XML
app:cardBackgroundColor="@android:color/red"
code
cardName.setCardBackgroundColor(ContextCompat.getColor(this, R.color.colorGray));
Source: Stackoverflow.com