Change The Background Color of Cardview Programmatically

Change the background color of CardView programmatically

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"

MaterialCardView change background color and stroke color programmatically

I believe that the problem is not in the MaterialCardView background/stroke methods; but that the used color shades in the layout are different than those used programmatically.

The used colors in layout:

app:cardBackgroundColor="#DB8A61"
app:strokeColor="#EBD3C7"

But programmatically, you use tagColor and add alpha channel to it with colorWithAlpha; you didn't provide the tagColor; but adding 128 alpha component would not change that much; and that is totally different than the layout colors that don't have alpha component at all.

So, to have the same colors programmatically, just reuse the same layout colors without alpha component:

tagView.card.setCardBackgroundColor(ColorStateList.valueOf(Color.parseColor("#DB8A61")))
tagView.card.setStrokeColor(ColorStateList.valueOf(Color.parseColor("#EBD3C7")))

CardView background color always white

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

dynamically change BackgroundColor of CardView

Providing background color to the child class of cardview will leave the padded parts in case if the card view has any, without color and this is not a good approach.

Dynamically change the card view color as below, assuming you have a adapter to load the list in the card view.

In constructor of adapters Viewholder class

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

In onBindViewHolder method of the adapter class :

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

Where holder is the object of your viewholder class.

How to change background color of CardView programmatically

I don't know of any particular reasoning.

However, if you're interested in hacking around this omission...

All the CardView does with this attribute is create a rounded rectangle drawable using the color, and then assigns it as the background of the CardView. If you really want to set the color programmatically, you could create a copy of RoundRectDrawableWithShadow, and then do this:

mCardView.setBackgroundDrawable(new MyRoundRectDrawableWithShadow(getResources(), color, radius));

You cannot subclass RoundRectDrawableWithShadow or use it directly because it is not public.

Change Raduis CardView Background Color

To change the background color of the CardView you have to use the method:

cardView.setCardBackgroundColor(ContextCompat.getColor(this,R.color.xxx));

Sample Image

How to change cardview background color Programmatically in Kotlin

I found it, I forgot the set

cardview.setCardBackgroundColor(Color)

Everything works fine now, the background of my cardview change.

CardView background color doesn't change even after using app:cardBackgroundColor property

I figured out the issue was the color set to an imageView inside the cardView was overlaying the color of the cardView.
Another possible cause of this kind of behaviour is if the cardView was inside a layout whose elevation (layout) is possibly greater than the cardView.

Thanks @Mike M for asserting that cardView doesn't disappoint in this case.

Cardview Set Background color

method setCardBackgroundColor takes color parameter which means that color represented as 4 byte integer in ARGB format, but you pass into method R.color.LightCyan which isn't color but index of color inside of application/system resources. To get color you should use Color.argb(int alpha, int red, int green, int blue) or Resources.getColor(int index, Theme theme) or use ContextCompat to use it on older platforms.

get Cardview Background Color

You can simply initialize a CardView and set an id:

as an example in your .xml file in res/layout:

<android.support.v7.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="10dp"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="@color/cardview_dark_background"
android:background="@color/cardview_dark_background">

and then initalize it in your Activity/Fragment so:

 CardView cardView = (CardView) findViewById(R.id.CardView);
cardView.getCardBackgroundColor();

note that this method returns a ColorStateList rather than a single color value

so to get a single color value just call:

int backgroundColor = cardView.getCardBackgroundColor().getDefaultColor();


Related Topics



Leave a reply



Submit