Search code examples
androidkotlincolorscard

CardBackgroundColor remains Grey when I want to set a custom color, whereas default colors works fine


CardBackgroundColor remains Grey when I want to set a custom color, whereas default colors works fine.

For instance, Card.setCardBackgroundColor(R.color."customcolor") don't set the custom color. The color is grey, whereas Card.setCardBackgroundColor(Color.RED) works fine.

I have tried:

  • To take color without alpha
  • to set to 0dp elevation
  • Card.setCardBackgroundColor(color."customcolor")

No results.

EDIT :

Card.setCardBackgroundColor(Color.parseColor("#FFFFFFFF")) works. But I have to hardcode the color. Any idea on a better way?


Solution

  • Heyy,

    You have to get the color from the resources like this

    Card.setCardBackgroundColor(getResources().getColor(R.color.'your color'));
    

    Edit

    As you are getting a context from your adapter, so

    Card.setCardBackgroundColor(context.getResources().getColor(R.color.'your color'));
    

    That works perfectly!!