Search code examples
androidandroid-color

Set background color textivew using parseColor


I'm trying to set a TextView background color using parseColor, but I'm recieving this error:

Expected a color resource id, but received an RGB color integer.

Can some one help me please?

textView.setBackgroundColor(ContextCompat.getColor(itemView.context, Color.parseColor(product.brand.color)))

The object product.brand.color = #123123 (This is an example color)


Solution

  • Replace:

    textView.setBackgroundColor(ContextCompat.getColor(itemView.context, Color.parseColor(product.brand.color)))
    

    with:

    textView.setBackgroundColor(Color.parseColor(product.brand.color))
    

    ContextCompat.getColor() returns the color associated with a color resource (e.g., R.color.primary). Color.parseColor() does not return a color resource ID, which is why you are getting the error. Instead, Color.parseColor() returns an actual color, which is what you want for setBackgroundColor() anyway.