kuemme01
kuemme01

Reputation: 472

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.

Upvotes: 5

Views: 6323

Answers (4)

MEGHA RAMOLIYA
MEGHA RAMOLIYA

Reputation: 1917

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))

Upvotes: 0

mihanovak1024
mihanovak1024

Reputation: 256

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)

Upvotes: 13

Reaz Murshed
Reaz Murshed

Reputation: 24211

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.

Upvotes: 1

Preetika Kaur
Preetika Kaur

Reputation: 2031

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.

Upvotes: -1

Related Questions