I'm trying to use Paint.setColor() with a color from res/values/colors.xml but it keeps coming out "grayish". If I use a string literal instead and use Paint.parseColor() it shows up correctly. What's going on?
onDraw()
p.setColor (Color.parseColor ("#82ef82")); // <- this works
p.setColor (R.color.PeaGreen); // <- this is gray
colors.xml
<color name="PeaGreen">#82ef82</color>
R.color.PeaGreen is not a color, it's a resource id for a color resource. But since colors are represented by integers and so are resource id's, it's not throwing any warning or error. You need to do a little bit of work to get the actual color from a resource id:
p.setColor(context.getResources().getColor(R.color.PeaGreen));
There are also a few color constants defined in the Color class and you can use them like so:
p.setColor(Color.RED);
Related
I'm having a play with Android Studio.
I'm trying to change the background colour. I've found how to change it but what's puzzling me is that if I substitute the definition (ie the actual hex code) of the colorAccent property I don't see the same colour.
This is the original, which uses #color/colorAccent, which ultimately is a hex code defined in the class R.color:
This is what I get when I substitute the hex code that colorAccent references:
As you can see, I get a shade of grey instead of a shade of red. Why is this?
Thanks.
[Android Studio 3.2]
It is not shade of red beacuse depend on your select.
You can change color in XML if you hardcode it.
click on side color on left of code :
then choose your color:
But if you set it like android:background="#color/colorPrimary"
you should change it in color XML
The hex value for colorAccent in the R.java doesn't refer to the color, but rather the automatically generated unique id for that XML attribute in colors.xml. As you can see, the hex values for the variables are all in-order and increasing by 1 each time.
The actual hex code for the color denoted by colorAccent is defined in colors.xml, and substituting that directly in the android:background attribute will work as you expect it to.
From these answers, I suppose I know how to grab the primary color. However, when I try to set the background color of the toolbar, I keep getting exception that the resourceId for the color I am specifying is not found
android.content.res.Resources$NotFoundException: Resource ID ...
I have tried
toolbar.setBackgroundResource(primaryColor);
and I have tried
toolbar.setBackgroundColor(primaryColor);
I think that in your case, you are passing the resource id of the color as the argument to the setBackgroundColor method. You want to pass the integer that the color actually evaluates to instead. There are a number of ways to do this depending on what information you have (hex string, rgb integer values, etc. Check out the docs for more info on defining a color value.
Assuming that your color is defined in colors.xml in your res/values folder, you should be able to simply use this:
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.primaryColor);
Otherwise, you'll have to define your color programmatically first as I mentioned.
I've a normal TextView:
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_heigh="wrap_content"
android:textColor="#color/text_on_background"/>
It references #color/text_on_background. I've a few build flavors of my app, each with different color scheme. So I need to change the color of the text sometimes. But sometimes not, and I want to use default inverse text color from theme.
So i tried these:
<color name="text_on_background">?android:textColorPrimaryInverse</color>
<color name="text_on_background">?android:attr/textColorPrimaryInverse</color>
And it always ended with error about inflating the TextView. And I couldn't find any #color/…inverse… or #android:color/…inverse….
Any clue how to do that?
If you want to inverse the Color means , you can do (MAX - COLOR_VAL) in all (R,G,B)
for example
I Assume F3FAB6 - this is your color code means you can do following way
Color.rgb(255-243, 255-250,255-182);
You can get Inverse Color.
I have color code #ffa100. I have also define it in values xml as
<color name="orange">#ffa100</color>
I want to find name "orange" based on the color code value "#ffa100" How can I find it? I want to use name of color in program from value where several colors are defined and I get has code from server.
You can set it directly from with the hex code, no need to have it in resources. Hex value has to be a String.
your_image.setBackgroundColor(Color.parseColor("#ffa100"))
Use,
Color.parseColor("#ffa100");
like,
mTextView.setTextColor(Color.parseColor("#ffa100")); //set text color
mTextView.setBackgroundColor(Color.parseColor("#ffa100")); // set background color
I defined a white color in mycolors.xml under res/values as below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="my_white">#ffffffff</color>
</resources>
In my code, I set the text color of a TextView as the white color I defined:
TextView myText = (TextView) findViewById(R.id.my_text);
myText.setTextColor(R.color.my_white);
But the text in the TextView turned out to be a black color.
When I use #color/my_white in layout xml files, it works fine.
I check the generate R.java, and see:
public static final int my_white=0x7f070025;
Did I miss something?
Thanks.
R.color.my_white is an ID that contains a reference to your resource. setTextColor expects you pass a color, not a reference. The code compiles and gives no errors because setTextColor expect an int; but, of course, you are giving the wrong int. In this case, you will have to convert the reference to a integer that represents that color:
Resources res = getResources();
int color = res.getColor(R.color.my_white);
myText.setTextColor(color);
Based on the Android developer docs, it looks like your reference should be:
myText.setTextColor(R.color.my_white);
ETA: As noted in Mayra's answer, R.id.my_white is probably returning a reference to the object that represents your colour rather than the ARGB value of the colour.
What you are providing setTextColor currently is the id of the my_white object, not the color my_white. I think what you want is R.color.my_white.