Get a color resource as a string
I am trying to use Color.parseColor() in a color resource:
<color name="redish">#FF0000</color> I tried this, but it gives me an unknown color error :
Color.parseColor(Integer.toHexString(context.getResources().getColor(R.color.redish))) How to correctly convert a color resource to String ?
I think you missed #
Color.parseColor("#"+Integer.toHexString(ContextCompat.getColor(context, R.color.redish))) context.getResources().getColor(R.color.redish)); Updated answer:
String colorHex = "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.colorPrimary) & 0x00ffffff); I got the color stored in the object (containing other fields). Colors were also defined in the xml file (colors.xml).
Therefore, I wanted to set the background color of the textview. I did it as follows:
... String color= res.colorName; // res is an object int c = context.getResources().getIdentifier(color,"color", context.getPackageName()); textView.setBackgroundColor(Color.parseColor("#" + Integer.toHexString(context.getResources().getColor(c)))); If you use the code in action, you can omit the use of "context".
