How to set the text color of TextView in code?

2018-12-31 10:12发布

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?

30条回答
路过你的时光
2楼-- · 2018-12-31 10:40
   textViewStatus.setTextColor(res.getColor(R.color.green));
查看更多
像晚风撩人
3楼-- · 2018-12-31 10:42

You should use:

holder.text.setTextColor(Color.RED);

For a sanity check, I just tried it, because I had a project open anyway, and yes, it's nice and red ;D


You can use various functions from the Color class to get the same effect of course.

  • Color.parseColor (Manual) (like LEX uses)

    text.setTextColor(Color.parseColor("#FFFFFF"));
    
  • Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses)

    holder.text.setTextColor(Color.rgb(200,0,0));
    holder.text.setTextColor(Color.argb(0,200,0,0));
    
  • And of course, if you want to define your color in an XML file, you can do this:

    <color name="errorColor">#f00</color>
    

    because the getColor() function is deprecated1, you need to use it like so:

    ContextCompat.getColor(context, R.color.your_color);
    
  • You can also insert plain HEX, like so:

    myTextView.setTextColor(0xAARRGGBB);
    

    Where you have an alpha-channel first, then the color value.

Check out the complete manual of course, public class Color extends Object.


1This code used to be in here as well:

textView.setTextColor(getResources().getColor(R.color.errorColor));

This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.

查看更多
爱死公子算了
4楼-- · 2018-12-31 10:43
TextView text = new TextView(context);
text.setTextColor(Color.parseColor("any hex value of a color"));

Above code is working on my side. Here text is a TextView on which color is needed to be set.

查看更多
泪湿衣
5楼-- · 2018-12-31 10:43

Similarly, I was using color.xml:

<color name="white">#ffffff</color>
    <color name="black">#000000</color>   

For setting the TextView background like:

textView.setTextColor(R.color.white);

I was getting a different color, but when I used the below code I got the actual color.

textView.setTextColor(Color.parseColor("#ff6363"));
查看更多
公子世无双
6楼-- · 2018-12-31 10:44

text.setTextColor(getResource().getColor(R.color.black)) you have create black color in color.xml.

OR

text.setTextColor(Color.parseColor("#000000")) here type desired hexcode

OR

text.setTextColor(Color.BLACK) you can use static color fields

查看更多
心情的温度
7楼-- · 2018-12-31 10:44

For providing rgb values: text.setTextColor(Color.rgb(200,0,0));
For parsing the color from a hex value: text.setTextColor(Color.parseColor("#FFFFFF"));

查看更多
登录 后发表回答