Change the background color of CardView programmatically
Solution 1
What you are looking for is:
CardView card = ...
card.setCardBackgroundColor(color);
In XML
card_view:cardBackgroundColor="@android:color/white"
Update: in XML
app:cardBackgroundColor="@android:color/white"
Solution 2
Use the property card_view:cardBackgroundColor:
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_gravity="center"
card_view:cardCornerRadius="4dp"
android:layout_margin="10dp"
card_view:cardBackgroundColor="#fff"
>
Solution 3
You can use this in XML
card_view:cardBackgroundColor="@android:color/white"
or this in Java
cardView.setCardBackgroundColor(Color.WHITE);
Solution 4
I used this code to set programmatically:
card.setCardBackgroundColor(color);
Or in XML you can use this code:
card_view:cardBackgroundColor="@android:color/white"
Solution 5
I was having a similar issue with formatting CardViews in a recylerView.
I got this simple solution working, not sure if it's the best solution, but it worked for me.
mv_cardView.getBackground().setTint(Color.BLUE)
It gets the background Drawable of the cardView and tints it.
Related videos on Youtube
Gabriele Mariotti
An experienced professional with more than 20 years of work in IT scope, with deep focus on develop and technical areas. Strong leadership with a proven ability to design software solutions and to manage teams. You can follow me on: Linkedin Twitter Github
Updated on July 15, 2021Comments
-
Gabriele Mariotti almost 3 years
The CardView has an attribute
card_view:cardBackgroundColor
to define the background color. This attribute works fine.At the same time there isn't a method to change the color 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/inside_layout"> </android.support.v7.widget.CardView> View insideLayout = mCardView.findViewById(R.id.inside_layout); cardLayout.setBackgroundColor(XXXX);
These solutions don't work because the card has a cardCornerRadius.
-
Gabriele Mariotti over 9 years+1 for hacking and I agree. I can't find a better solution. Anyway I hope in something different in official API.
-
Napolean over 9 yearsIs this class(RoundRectDrawableWithShadow or RoundRectDrawable) accessible? I don't think so
-
Gabriele Mariotti over 9 yearsIt seems a new method in support lib. I didn't see it the last month, may be it is an update
-
Gabriele Mariotti over 9 yearsYou are adding a LL in your cardview in this way and of course you can do it, but it doesn't answer the original question.
-
Martial Konvi over 9 yearsThe request was about changing the card background color dynamically, and i think this method does it pretty simply by changing the inner layout background color. Or am i missing something.
-
Wirling over 8 yearsThanks for pointing out the XML styling. Always struggling with that. Isn't there a way to define the style in themes.xml? Then you wouldn't have to set the style for each CardView.
-
chntgomez about 8 yearsThis answer is more usefull because contains the static (XML) and the programmatic way (JAVA) Kudos.
-
Mavamaarten almost 8 yearsMight want to add that the way to configure this in XML is
card_view:cardBackgroundColor="@android:color/white"
-
rsicarelli over 7 yearsUsing
card_view
namespace doesn't work for me, I have to useapp
instead. -
Jim Andreas over 7 yearsThat is pretty awesome - I was struggling with trying to figure out how to propagate selectors down through the layout - and this was NOT working pre API21. The ripple was working fine post API21- but your theme approach was the first time I managed to get the CardView to give the right look on clicking across the contents.
-
Bryan Bryce about 7 years@rsicarelli All depends on what you name it:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
-
Bhaumik Ghodasara about 5 yearsHow can i set Gradient Drawable to card background programmatically ?
-
stramin almost 5 yearsThe OP tell us about that property in the first line of his question, he said he wants to achieve it "dinamically".
-
stramin almost 5 yearsThe OP tell us about that property in the first line of his question, he said he wants to achieve it "dinamically".
-
stramin almost 5 yearsThe OP tell us about that property in the first line of his question, he said he wants to achieve it programmatically/dinamically.