Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the color of a TextView in Android?

Tags:

android

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?

like image 326
Arun Avatar asked Sep 08 '10 14:09

Arun


People also ask

How do I change the color of TextView?

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.

How do I change the text color on my Android?

Open your device's Settings app . Text and display. Select Color correction. Turn on Use color correction.


1 Answers

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)) 
like image 130
Patrick Cullen Avatar answered Sep 20 '22 07:09

Patrick Cullen