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?
Using Adapter you can set the text color by using this code:
If you still want to specify your colors in your XML file:
Then reference it in your code with one of these two methods:
or
The first is probably preferable if you're compiling against Android M, however the theme you pass in can be null, so maybe that's easier for you?
And if you're using the Compat library you can do something like this
Use:
or
use the following code in layout.xml