Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the text color of TextView in code?

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?

like image 364
Vikas Avatar asked Jan 05 '11 10:01

Vikas


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.

Which property is used to set the color of the text in TextView?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .

Can we change the text in TextView?

TextView tv1 = (TextView)findViewById(R. id. textView1); tv1. setText("Hello"); setContentView(tv1);

How do I change the color of my text messages on android?

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


2 Answers

You should use:

holder.text.setTextColor(Color.RED); 

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.

like image 96
Nanne Avatar answered Sep 21 '22 12:09

Nanne


If you still want to specify your colors in your XML file:

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

Then reference it in your code with one of these two methods:

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

or

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

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

textView.setTextColor(ContextCompat.getColor(context, R.color.errorColor)); 
like image 35
xbakesx Avatar answered Sep 19 '22 12:09

xbakesx