Rectangle 27 0

android How to set the text color of TextView in code?


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="textbody">#ffcc33</color>

</resources>
android:textColor="@color/textbody"
final TextView tvchange12 = (TextView) findViewById(R.id.textView2);
//Set color for textbody from color.xml file
tvchange1.setTextColor(getResources().getColor(R.color.textbody));

Create a color.xml file in the values folder:

Or you can use this color in a Java file:

Then in any XML file, you can set color for text using,

You can do this only from an XML file too.

Note
Rectangle 27 0

android How to set the text color of TextView in code?


text.setTextColor(Color.argb(0,200,0,0));
text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
<color name="color_purple">#800080</color>
text.setTextColor(0xAARRGGBB);
text.setTextColor(getResources().getColor(R.color.color_purple)
textView.setTextColor(Color.parseColor("#ffffff"));
  • You can also use RGB text.setTextColor(Color.rgb(200,0,0));

Add color value in studio res->values->colors.xml as

And if you're using the Compat library you can do something like this

Now set the color in xml or actvity class as

Use can also use direct hexcode for textView. You can also insert plain HEX, like so:

You can also use argb with alpha values.

a for Alpha (Transparent) v.

Note
Rectangle 27 0

android How to set the text color of TextView in code?


<color name="errorColor">#f00</color>
Color.argb
Color.parseColor
Color.rgb
ContextCompat.getColor(context, R.color.your_color);
getColor(int)
holder.text.setTextColor(Color.rgb(200,0,0));
holder.text.setTextColor(Color.argb(0,200,0,0));
myTextView.setTextColor(0xAARRGGBB);
text.setTextColor(Color.parseColor("#FFFFFF"));
textView.setTextColor(getResources().getColor(R.color.errorColor));

1This code used to be in here as well:

@Nanne It would be nice if your answer also mentioned that R.color.XXX is a reference to the color. Meaning that it needs to be dereferenced (as it is in your example), for clarity.

And of course, if you want to define your color in an XML file, you can do this:

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

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

I'm not sure what you mean? As in, dereferenced and so will use more resources or do you mean something else?

In addition, if the text is a link you need to use text.setLinkTextColor(...); in code or android:textColorLink="..." in XML

Is there any way of telling if a particular color value is going to let a text disappear?

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.

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

You can also insert plain HEX, like so:

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

You should use:

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

Note
Rectangle 27 0

android How to set the text color of TextView in code?


myTextView.setTextColor(0xAARRGGBB);

And why the down-vote? Can you add some thoughts on this please?

Note
Rectangle 27 0

android How to set the text color of TextView in code?


to my experience, that's not true and it is possible to use a RGB value instead of a ARGB value

Note
Rectangle 27 0

android How to set the text color of TextView in code?


TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(285,0,0));
Note
Rectangle 27 0

android How to set the text color of TextView in code?


textViewStatus.setTextColor(res.getColor(R.color.green));
Note
Rectangle 27 0

android How to set the text color of TextView in code?


<TextView  android:id="@+id/textView1"    
android:layout_width="wrap_content"    
android:layout_height="wrap_content" 
android:text="@string/add"
android:layout_marginTop="16dp"
android:textAppearance="?
android:attr/textAppearanceMedium"
android:textColor="#25383C"
android:textSize="13sp" />

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add"
        android:layout_marginTop="16dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#25383C"
        android:textSize="13sp" />

use the following code in layout.xml

Note
Rectangle 27 0

android How to set the text color of TextView in code?


textView.setTextColor(Color.parseColor("#ffffff"));
textView.setTextColor(R.color.white);

or if you want to give color code from colors folder then use

Note
Rectangle 27 0

android How to set the text color of TextView in code?


holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
holder.text_view.setTextColor(Color.parseColor("#FF00FF"));

Using Adapter you can set the text color by using this code:

Note