How to pass color resource as parameter (Android)
Asked Answered
D

3

6

This could be the simplest thing ever but for the life of me I haven't figured it out just yet.

I have a method that sets the background color of layout but I want to pass the color as a parameter like we do with drawable resources. eg

public void setIcon (Drawable icon){
  this.icon = context.getResources().getDrawable(icon);
}

setIcon(R.drawable.tuborg);

I want to be able to do something similar with color (R.color.id). I've tried

public void setColor (Color color){
  layout.setBackgroundColor(context.getResources().getColor(color));
}

and

public void setColor (Color color){
  layout.setBackgroundColor(ContextCompat.getColor(color));
}

both of which are asking for int, even (int color) doesn't work. Plus I'm trying to avoid Color.parse().

This is how I'm using the function

setColor(R.color.colorAccent);

I have an xml with various color codes. I want to be able just call this function and get the background color change.

Damon answered 23/11, 2016 at 18:34 Comment(7)
public void setColor(int color) { layout.setBackgroudColor(color); }Concretize
thanks @Concretize but this does't work. I stated that in the question...Damon
You are assigning value to layout.setBackgroundColor, instead of that setting color directly is not working ??Concretize
you are almost there. you don't need Color - colors are actually represented as int (notice that getColor returns an int and setBackgroundColor takes an int), but what may be confusing is how getColor also takes an int as a parameter - its expecting the resource ID of the color defined in xml. So you can simply pass the resourceID and use getColor on it, OR, you can pass the color itself (as an int) and use setBackgroundColor on it directly.Ingrowing
so how do i pass that in a function @IngrowingDamon
@MueyiwaMosesIkomi I have updated my answer. Please have a look.Patriliny
Thanks guys for all the help. Finally got it work. All your answers and suggestions where interesting but i went with the one that was closest to my usecase. Thanks a bunch..Damon
M
9

You can try this out:

public void setColor (int colorId){
  layout.setBackgroundColor(ContextCompat.getColor(colorId));
}

In that method colorId should be an hexa code of the color

A good practice is to define the color on colors.xml (inside values folder).

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <color name="red">#FF0000</color>
</resources>

In this case, you will use this function like this:

setColor(R.color.red);

So, there is no need to create a "color" object, you can pass values from colors.xml

Also, in your case you should modify the method setColor(Color aColor) to setColor(int aColor) to make it work with the xml color resource.

Moria answered 23/11, 2016 at 18:51 Comment(4)
Doesnt work, expects a resource of type color. Yes i have the xml. That's what i'm trying to referenceDamon
@MueyiwaMosesIkomi are you referencing by doing this R.color.your_color_in_colors_xml? Could you tell me which is the class of "layout"?Moria
@MueyiwaMosesIkomi I have edited my answer with this: Also, in your case you should modify the method setColor(Color aColor) to setColor(int aColor) to make it work with the xml color resource.Moria
I think ContextCompat.getColor(colorId) needs a context so it may be like this ContextCompat.getColor(MainActivity.this, colorId)Canso
P
8

You need a color resource id. It starts with R.color which is actually an integer id.

public void setColor (@ColorInt int colorId){ // integer id 
  layout.setBackgroundColor(ContextCompat.getColor(colorId));
}

UPDATE

Although you are using it like setColor(R.color.colorAccent) but still your function parameter expects a color e.g.

setColor (Color color)

. You need to update the function parameter type to int, like I did in the above snippet.

setColor (int color)

Also from the docs of ContextCompat.getColor.

Returns a color associated with a particular resource ID.Starting in {@link android.os.Build.VERSION_CODES#M}, the returned color will be styled for the specified Context's theme.

@param id The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.

@return A single color value in the form 0xAARRGGBB.

Patriliny answered 23/11, 2016 at 18:40 Comment(0)
A
3

For one, it looks like you're trying to set properties of the View which is not a concept that exists in Java. So what you really want is:

layout.setBackgroundColor(context.getResources().getColor(R.color.colorRes));

View#setBackgroundColor() expects a color integer in the format #AARRGGBB in which

A = alpha R = red G = green B = blue.

So to set the color pure red you can do something like this:

layout.setBackgroundColor(0xFFFF0000);

Pure blue:

layout.setBackgroundColor(0xFF0000FF);

Pure green:

layout.setBackgroundColor(0xFF00FF00);

For Resources#getColor(int);, it is expect a Resource ID of the color you want. In this case, you would put colors.xml in the res/values folder of your project. Then have something like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
</resources>

Then you can retrieve the color like so:

int color = context.getResources().getColor(R.color.colorPrimary);

Or if you're on API 23 or higher:

int color = context.getColor(R.color.colorPrimary);

Or the more simple ContextCompat method:

int color = ContextCompat.getColor(R.color.colorPrimary);

Then you can set the background color with it like so:

view.setBackgroundColor(color);

And finally, if you don't want to do any of that, you can simply set it directly on the View like so:

view.setBackgroundResource(R.color.colorPrimary);
Awestricken answered 23/11, 2016 at 18:41 Comment(3)
so there's really no way to append R.color.id to context.getResources().getColor(R.id.colorRes);Damon
@MueyiwaMosesIkomi Not sure what you mean by that. The R.id is a resource ID. It doesn't mean anything beyond that. It's only meant to link to the resource. Also, I messed up. It's supposed to be R.color. since it's a color resource.Awestricken
@MueyiwaMosesIkomi: I've edited the answer quite a bit. Hopefully you'll find something in there that helps.Awestricken

© 2022 - 2024 — McMap. All rights reserved.