kuemme01 kuemme01 - 2 months ago 12
Android Question

Possible to get color with string?

In my

values
folder I have
my_colors.xml
:

<resources>
<!-- Orange -->
<color name="orangePrimary">#f6a02d</color>
<color name="orange1">#e3952a</color>
<color name="orange2">#da8f28</color>
<color name="orange3">#d08926</color>
</resources>


Is there a way to get these colors just with the string of its name?
Something like
view.setBackgroundColor.getColor("orange1");


For images you have this
getResources().getIdentifier("my_image", "drawable", getPackageName());


Hope you guys know what I mean.
Greetings.

Answer

Have you tried the following:

int desiredColour = getResources().getColor(getResources().getIdentifier("my_color", "color", getPackageName()));

Hope it helps!