In XML, we can set a text color by the textColor
attribute, like android:textColor="#FF0000"
. But how do I change it by coding?
I tried something like:
holder.text.setTextColor(R.color.Red);
Where holder
is just a class and text
is of type TextView
. Red is an RGB value (#FF0000) set in strings.
But it shows a different color rather than red. What kind of parameter can we pass in setTextColor()? In documentation, it says int
, but is it a resource reference value or anything else?
In the
colors.xml
file, write in the code below:And another one:
I normally do this for any views:
where
AA defines alpha (00 for transparent, FF for opaque)
RRGGBB defines the normal HTML color code (like FF0000 for red).
You can do this only from an XML file too.
Create a
color.xml
file in the values folder:Then in any XML file, you can set color for text using,
Or you can use this color in a Java file:
I believe that if you want to specify a color as a resource (in the XML file), you'll have to provide its ARGB value (not simply the RGB value).
Try changing your color value to
#FFFF0000
. It should give you RED.