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.
Color
- colors are actually represented asint
(notice that getColor returns an int and setBackgroundColor takes an int), but what may be confusing is howgetColor
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 usegetColor
on it, OR, you can pass the color itself (as an int) and usesetBackgroundColor
on it directly. – Ingrowing