5

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.

4 Answers 4

13

Have you tried the following:

// java Resources res = context.getResources(); String packageName = context.getPackageName(); int colorId = res.getIdentifier("my_color", "color", packageName); int desiredColor = res.getColor(colorId); 
// kotlin val res: Resources = context.getResources() val packageName: String = context.getPackageName() val colorId: Int = res.getIdentifier("my_color", "color", packageName) val desiredColor: Int = res.getColor(colorId) 

Hope it helps!


Note: This is deprecated, instead you could do the following, which handles both pre and post Marshmallow (API 23):

// java Resources res = context.getResources(); String packageName = context.getPackageName(); int colorId = res.getIdentifier("my_color", "color", packageName); int desiredColor = ContextCompat.getColor(context, colorId); 
// kotlin val res: Resources = context.getResources() val packageName: String = context.getPackageName() val colorId: Int = res.getIdentifier("my_color", "color", packageName) val desiredColor: Int = ContextCompat.getColor(context, colorId) 
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, Reaz Murshed suggested that before he changed his answer. Your "desiredColour" is always wrong. I don't know why, but in my case its always that dark transparent purple.
Of course, I have updated my answer. You firstly get the resource id, then you must get the color from the resource id.
UUhhhh, thats working! But didn't I get problems in the future with that deprecated method getColor() ?
You should be fine for now, if it causes any problems in the future, you can still fix the problem.
Could you give me the Kotlin version of that please. stackoverflow.com/questions/65445066/…
|
1

Okay, I got the color by name using reflection now and got this working in my side.

You need to write a function like 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; } 

Now get the resource id using

int resourceId = getColorByName("orange1"); 

And set the drawable as a resource in your ImageView like this.

imageView.setBackgroundResource(resourceId); 

I tried setting img.setBackgroundColor(resourceId) which was setting the wrong color.

In your case I would like to suggest to keep the colors in a typed array in your res/values/arrays.xml like this

<array name="colors"> <item>#FFFF0000</item> <item>#FF00FF00</item> <item>#FF0000FF</item> </array> 

See the developers doc for Typed Array about how to use it.

4 Comments

Yeah, tried that, put returned the wrong color. A color I'm not created, some kind of dark transparent purple.
See the updated answer please. Got it working in my side.
Thank you, looks nice and clean. Unfortunately its not working for me. I need to change the color of my statusbar, some views and some imageviews. So maybe .setBackgroundResource working but setStatusBarColor didn't. I think I have to make some kind of HashMap<String, Integer> that store my colors I think =(
Okay, I tried that, but I get the same problem. To choose the right array I need to type R.array.orange for example so its the same as in the beginning. But I vote that up. Maybe someone elses problem solved with your solution. I make a HashMap with my colors now.
0
if you are using only R it might take material base Resource( com.google.android.material.R.color). so, you need to use below the code with your package name Resource(com.example.app.R.color) button.setBackgroundColor(context.resources.getColor(com.example.app.R.color.colorPrimary)) 

Comments

-1

Starting from Android Support Library 23, a new getColor() method has been added to ContextCompat.

So, just call:

ContextCompat.getColor(context, R.color.your_color); 

The other one is depracated getResources.getColor() So you need to implement the above. No any way for just passing the name of color to access it. you have to give color id from your color file.

1 Comment

thats so uncool! I have 8 different colors each with 4 different color temperatures. For knowing which one is needed I get a string like "orange".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.