1

I have some colors defined in /values/colors.xml.

How can I programmatically get the id of a certain color e.g. R.color.my_color if I know the name of the color.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
dumazy
  • 13,857
  • 12
  • 66
  • 113

4 Answers4

6

Try this:

public int getColorByName( String name ) {
    int colorId = 0;

    try {
        Class res = R.color.class;
        Field field = res.getField( name );
        colorId = field.getInt(null);
    } catch ( Exception e ) {
        e.printStackTrace();
    }

    return colorId;
}

and in your case name is my_color:

getColorByName("my_color");
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
3

There is a dedicated method in Resources called getIdentifier.
It's the "normal" way of achieving what you search.

Try

final int lMyColorId = this.getResources().getIdentifier("my_color", "color", this.getPackageName());

where this is an Activity or any Context subclass reference. (Replace by getActivity() if needed.)
This is said to be slow but imo, this shouldn't be slower than accesing fields through the reflection mechanism the accepted answer suggests.

Example of use for some resource types are described here.

Community
  • 1
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Fabio Antunes Mar 27 '14 at 14:22
  • This has been updated. And by the way as my answer was right, yet incomplete, you have no legitimacy in downvoting it. Especially thinking it's my first answer on this site, such behaviour is very discouraging. – ultranestor Mar 27 '14 at 14:59
  • For your information I didn't down vote you. I just reviewed your answer and guide you to the right direction. – Fabio Antunes Mar 27 '14 at 15:03
1

once you have the Context you can call getResources()-- to get Resources reference and thereafter query it to get both color and the id of the resource.

rock_win
  • 755
  • 1
  • 5
  • 14
0

I found the accepted answer is not working as when I tried to set the background of an ImageView it was not setting the right color to it. But then I tried setting the background as resource and it worked perfectly.

So in case of any other confusions, I just want to copy the answer of @MarcinOrlowski and put all these together here.

So here's the function using reflection to get the resource id of the color.

public int getColorByName(String name) {
    int colorId = 0;

    try {
        Class res = R.color.class;
        Field field = res.getField(name);
        colorId = field.getInt(null);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return colorId;
}

So now you can get the resource id simply by calling this.

int resourceId = getColorByName("my_color");

And while you're setting this color using the resource id you've got here, you need to do this.

myImageView.setBackgroundResource(resourceId);

I tried setting myImageView.setBackgroundColor(resourceId) which was not working.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98