i need to get the string value of a color. in other words i want to pull the #ffffffff from a color resource like <color name="color">#ffffffff</color> in string format. is there any way of doing this?
5 Answers
Assuming you have:
<color name="green">#0000ff00</color> And here is code:
int greenColor = getResources().getColor(R.color.green); String strGreenColor = "#"+Integer.toHexString(greenColor); 1 Comment
Ryan M
This is not a good idea, as Integer.toHexString will not restore the leading zeroes. If you're not using alpha, it will work, as the alpha is set to FF and there are no leading zeroes, but in the example given, strGreen color will be "#ff00", not "#0000ff00" as intended,
Integer.toHexString(ContextCompat.getColor(context, R.color.black) & 0x00ffffff);