kuemme01 kuemme01 - 1 year ago 68
Android Question

Possible to get color with string?

In my

folder I have

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

Is there a way to get these colors just with the string of its name?
Something like

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

Hope you guys know what I mean.

Answer Source

Have you tried the following:

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

Hope it helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download