Question Detail

The CardView has an attribute card_view:cardBackgroundColor to define the background color. This attribute is working fine but i want this functionality dynamically.

I've just tried solutions like:

mCardView.setBackgroundColor(...);

or using a Layout inside the cardView

   <android.support.v7.widget.CardView>
        <LinearLayout
            android:id="@+id/cardView_ID">
    </android.support.v7.widget.CardView>  

 View view = mcardView.findViewById(R.id.cardView_ID);
 cardLayout.setBackgroundColor(XXXX);

These solutions don't work because the card has a cardCornerRadius.


Thread Reply

Anonymous

- 4 months ago

if you want to Change CardView Background  Color or Card Color programmatically follow below code :

CardView cardView = findviewbyid(R.id.cardView_ID)
cardView.setCardBackgroundColor(Color.RED);

In XML

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView_ID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardCornerRadius="2dp"
    android:layout_margin="3dp"
    card_view:cardBackgroundColor="#FFDA3C43"
    >
....
</android.support.v7.widget.CardView>