Dynamically change BackgroundColor CardView

The documentation says that BackgroundColor CardView can be installed in XML format with card_view:cardBackgroundColor. However, I cannot find an appropriate method for dynamically changing the background.

Using it mCardView.setBackgroundColor(Color.argb(75, 102,80,67));will cause CardView to lose its rounded corner and shadow.

+4
source share
3 answers

Providing a background color for a child class of the map will leave parts filled in if the view of the map has any color, and this is not a good approach.

Dynamically change the color of the map as shown below, provided that you have an adapter to download the list as a map.

Viewholder

mCardView = (CardView) itemView.findViewById(R.id.card_view);

onBindViewHolder :

holder.mCardView.setCardBackgroundColor(Color.GREEN); // will change the background color of the card view to green

.

+6

, , CardView:

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/card_layout"
            android:layout_width="match_parent"
            android:layout_height="72dp"/>
    </android.support.v7.widget.CardView>

    View cardLayout = mCardView.findViewById(R.id.card_layout);
    cardLayout.setBackgroundColor(Color.GREEN);
+1

An upcoming release will feature an API. So far, your only option is to use XML.

-2
source

All Articles