Binoy Babu Binoy Babu - 2 months ago 12
Android Question

There is no method to get the current background color of CardView

There is no method to get the current background color of CardView. There is a method to set the background like

cardView.setCardBackgroundColor(color);


I would like something like:

cardView.getCardBackgroundColor();


It will be really helpful.

Answer

It looks like a getCardBackgroundColor() method has indeed been added to CardView, but in a rather recent version, so just make sure your support library version is up to date.

Do note that this method returns a ColorStateList rather than a single color value. Calling getDefaultColor() on that will give you the normal background color.

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