Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a color ID by String value?

I have a String that has a value of "c104":

String color = "c104"; 

and I have a color with the name "c104":

<color name="c104">#000000</color>

How can I get that color by that String value?

I can't make it like R.id.color;

Is there any way to convert that String to an ID?

like image 966
Maysara Alhindi Avatar asked Apr 19 '16 18:04

Maysara Alhindi


1 Answers

There is a getIdentifier() method to retrieve the resource ID for arbitrary resources, including colors:

String colorName = "c104";
int colorResId = getResources().getIdentifier(colorName, "color", getPackageName());

That would be the same as:

int colorResId = R.color.c104;
like image 135
Floern Avatar answered Oct 01 '22 23:10

Floern