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?
Use ResourcesCompat. getColor(App. getRes(), R. color.
Note: A color is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine color resources with other simple resources in the one XML file, under one <resources> element.
A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied.
Assuming you have:
<color name="green">#0000ff00</color>
And here is code:
int greenColor = getResources().getColor(R.color.green);
String strGreenColor = "#"+Integer.toHexString(greenColor);
If you need just the HEX value (without alpha):
int intColor = getResources().getColor(R.color.your_color);
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));
You won't be able to pull out the original source text of the XML. That is converted to a binary value at build time. (So, for instance, the difference between #fff
and #ffffffff
is erased.)
You can convert the color value to a hex string, of course, using Integer.toHexString(int)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With