In the string.xml file I use the following tag
<color name="mycolor1">#F5DC49</color>
If I use
textview1.setTextColor(Color.CYAN);
it works, but
textview1.setTextColor(R.color.mycolor1);
is not working.
How can I use the color defined in the XML file?
TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.
Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.
TextView.setTextColor()
takes an int representing the color (eg. 0xFFF5DC49) and not the resource ID from the xml file. In an activity, you can do something like:
textView1.setTextColor(getResources().getColor(R.color.mycolor))
outside of an activity you'll need a Context
eg.
textView1.setTextColor(context.getResources().getColor(R.color.mycolor))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With