611

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vikas
  • 24,082
  • 37
  • 117
  • 159
  • A note about tweaking UI in code, please consider the advantages of seeing the UI in design time, minimizing the runtime changes to minimum. – AlikElzin-kilaka May 18 '16 at 05:05

40 Answers40

1369

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.

magnussn
  • 19
  • 5
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • 2
    In addition, if the text is a link you need to use text.setLinkTextColor(...); in code or android:textColorLink="..." in XML – WOUNDEDStevenJones Oct 18 '12 at 22:46
  • 1
    @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. – nyaray Aug 14 '13 at 23:52
  • I'm not sure what you mean? As in, dereferenced and so will use more resources or do you mean something else? – Nanne Aug 15 '13 at 06:41
  • Is there any way of telling if a particular color value is going to let a text disappear? – Christopher Masser Dec 02 '13 at 12:41
  • 8
    `getColor(int)` is deprecated. – RestInPeace Nov 28 '15 at 10:05
  • @AlwinKesler I'm not really sure what you mean by posting that link. **It was allready in the answer, in code and as a link**. I did change it a bit because it seems that it wasn't clear enough, so maybe this is better? – Nanne Feb 10 '17 at 10:24
  • 1
    Sorry, I want's clear enough. On Android 6.0 (API 23) `getResource().getColor()` is deprecated. Consider using `ContextCompat.getColor()` instead – Alwin Kesler Feb 10 '17 at 14:04
  • textView.setTextColor(ContextCompat.getColor(getApplicationContext(),R.color.colorWhite)); Add below line in colors.xml. #FFFFFF – Kaushik Chatterjee Jan 04 '18 at 14:04
  • 1
    how did to import color class? – jorghe94 Apr 04 '18 at 17:53
  • More codes about color codes in Android studio https://encycolorpedia.com/a4c639 – Dinith Oct 30 '19 at 08:29
  • Fun Fact: You can't set an Emoji's color, even though it's a member of your font. Call all the other glyphs "inked," and changing the color changes their ink. But all Emojis come with their own specified colors (which hopefully work on both light and dark backgrounds), so setTextColor() just bounces off them. – Phlip Dec 25 '20 at 10:30
149

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));
xbakesx
  • 13,202
  • 6
  • 48
  • 76
  • 10
    For setTextColor, why does it have to take the getResources().getColor() rather than the direct R.color.errorColor reference? The R.color.x works for almost every other method. Incredibly frustrating! – Civilian Jul 20 '11 at 21:57
  • 9
    @Civilian: because the int param that the setXXXColor() methods require is taken as the actual ARGB value to use, NOT the value to lookup in the resources file. Strangely enough, the View class has both setBackgroundColor() and setBackgroundResource(), while TextView is missing a setTextResource() method. – Ian Kemp Oct 27 '11 at 20:51
  • 3
    `getColor(int)` is deprecated. `ContextCompat.getColor(getContext(), R.color.yourColor);` seems to be the replacement. – RestInPeace Nov 28 '15 at 10:07
55

And another one:

TextView text = (TextView) findViewById(R.id.text);
text.setTextColor(Color.parseColor("#FFFFFF"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LEX
  • 634
  • 1
  • 7
  • 11
  • 1
    `getResources()` is a Context member function. For adapters, use `getContext().getResources()`. Color values should go into resources, like in @xbakesx's answer. – C0D3LIC1OU5 Nov 18 '15 at 21:12
44

You can do this only from an XML file too.

Create a color.xml file in the values folder:

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

</resources>

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

android:textColor="@color/textbody"

Or you can use this color in a Java file:

final TextView tvchange12 = (TextView) findViewById(R.id.textView2);
//Set color for textbody from color.xml file
tvchange1.setTextColor(getResources().getColor(R.color.textbody));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
newday
  • 3,842
  • 10
  • 54
  • 79
29

You can use

holder.text.setTextColor(Color.rgb(200,0,0));

You can also specify what color you want with Transparency.

holder.text.setTextColor(Color.argb(0,200,0,0));

a for Alpha (Transparent) value r-red g-green b-blue

bkaid
  • 51,465
  • 22
  • 112
  • 128
Ganapathy C
  • 5,989
  • 5
  • 42
  • 75
19

use the following code in layout.xml

<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" />
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
Chinna
  • 191
  • 1
  • 3
16

There are many different ways to set color on text view.

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

    <color name="color_purple">#800080</color>
    

    Now set the color in xml or actvity class as

    text.setTextColor(getResources().getColor(R.color.color_purple)
    
  2. If you want to give color code directly use below Color.parseColor code

    textView.setTextColor(Color.parseColor("#ffffff"));   
    
  3. You can also use RGB

    text.setTextColor(Color.rgb(200,0,0));
    
  4. Use can also use direct hexcode for textView. You can also insert plain HEX, like so:

    text.setTextColor(0xAARRGGBB);
    
  5. You can also use argb with alpha values.

       text.setTextColor(Color.argb(0,200,0,0));
    

    a for Alpha (Transparent) v.

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

       text.setTextColor(ContextCompat.getColor(context, R.color.color_purple));
    
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Rank
  • 900
  • 9
  • 17
10
textView.setTextColor(ContextCompat.getColor(getApplicationC‌​ontext(),R.color.col‌​orWhite)); 

In the colors.xml file, write in the code below:

<color name="colorWhite">#FFFFFF</color>
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
9

I normally do this for any views:

myTextView.setTextColor(0xAARRGGBB);

where

  • AA defines alpha (00 for transparent, FF for opaque)

  • RRGGBB defines the normal HTML color code (like FF0000 for red).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
A.W
  • 99
  • 1
  • 3
7

If you plan to use setTextAppearance you should know that it will overwrite the text color with the style inherited from the theme. So if you want to use both, set the color afterwards.

This works:

textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);
textView.setTextColor(Color.RED);

While this will cause your textcolor to be for instance white(for dark theme) or black(for the light theme):

textView.setTextColor(Color.RED);
textView.setTextAppearance(context, android.R.style.TextAppearance_Medium);

Contrary to this in XML the order is arbitrary.

Robin Gawenda
  • 559
  • 5
  • 21
7

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

Ronnie Oosting
  • 1,252
  • 2
  • 14
  • 35
Nitish
  • 313
  • 4
  • 11
6

I believe that if you want to specify a color as a resource (in the XML file), you'll have to provide its ARGB value (not simply the RGB value).

Try changing your color value to #FFFF0000. It should give you RED.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69
5

Use:

TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(285,0,0));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Talha
  • 18,898
  • 8
  • 49
  • 66
5
holder.text.setTextColor(Color.rgb(200,0,0));

or

myTextView.setTextColor(0xAARRGGBB);
Pang
  • 9,564
  • 146
  • 81
  • 122
Yash Patil
  • 469
  • 4
  • 9
5

Kotlin Extension Solution

Add these to make changing text color simpler

For setting ColorInt

myView.textColor = Color.BLACK // or Color.parseColor("#000000"), etc.

var TextView.textColor: Int
get() = currentTextColor
set(@ColorInt color) {
    setTextColor(color)
}

For setting ColorRes

myView.setTextColorRes(R.color.my_color)

fun TextView.setTextColorRes(@ColorRes colorRes: Int) {
    val color = ContextCompat.getColor(context, colorRes)
    setTextColor(color)
}
Gibolt
  • 42,564
  • 15
  • 187
  • 127
4

if you use Kotlin, there are 4 ways: (with Holder)

  1. Use Android resource:

    holder.textView.setTextColor(Color.GREEN)

  2. Use RGB:

    holder.textView.setTextColor(Color.rgb(255, 87, 34))

3)Use Hex:

holder.textView.setTextColor(Color.parseColor("#C2185B"))

4)Use Project resource: (requires API level 23)

holder.textView.setTextColor(context.resources.getColor(R.color.colorMax,null))
Mori
  • 2,653
  • 18
  • 24
3

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

holder.text_view = (TextView) convertView.findViewById(R.id.text_view);
holder.text_view.setTextColor(Color.parseColor("#FF00FF"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
3
text1.setTextColor(Color.parseColor("#000000"));
Pang
  • 9,564
  • 146
  • 81
  • 122
  • [ask questions wisely](http://stackoverflow.com/questions/40479689/opencv-in-android-application-image-blur-detection) :) – Maveňツ Nov 08 '16 at 05:49
3
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.

karanatwal.github.io
  • 3,613
  • 3
  • 25
  • 57
2

From API 23 onward, getResources().getColor() is deprecated.

Use this instead:

textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.color_black));
JanS
  • 2,065
  • 3
  • 27
  • 29
PJ2104
  • 132
  • 7
1

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

holder.my_text_view = (TextView) convertView.findViewById(R.id.my_text_view);
holder.my_text_view.setTextColor(Color.parseColor("#FFFFFF"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1
   textViewStatus.setTextColor(res.getColor(R.color.green));
zudo1337
  • 21
  • 3
1

if you want to give color code directly then use

textView.setTextColor(Color.parseColor("#ffffff"));

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

textView.setTextColor(R.color.white);
reshma
  • 19
  • 3
  • this code textView.setTextColor(R.color.white); doesn't work. You could use text.setTextColor(getResources().getColor(R.color.color_purple) for getting the color from your color.xml – Hanako Sep 14 '18 at 23:13
1

I did this way: Create a XML file, called Colors in res/values folder.

My Colors.xml:

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="vermelho_debito">#cc0000</color>
    <color name="azul_credito">#4c4cff</color>
    <color name="preto_bloqueado">#000000</color>
    <color name="verde_claro_fundo_lista">#CFDBC5</color>
    <color name="branco">#ffffff</color>
    <color name="amarelo_corrige">#cccc00</color>
    <color name="verde_confirma">#66b266</color>
</resources>

To get this colors from the xml file, I've used this code: valor it's a TextView, and ctx it's a Context object. I'm not using it from an Activity, but a BaseAdapter to a ListView. That's why I've used this Context Object.

valor.setTextColor(ctx.getResources().getColor(R.color.azul_credito));

Hope it helps.

Cristiano Guerra
  • 615
  • 1
  • 7
  • 12
1

In order to set color of a TextView, TextView.setTextColor(R.color.YOURCOLOR) is not enough!

It has to be used like this –

TextView myText = (TextView) findViewById(R.id.YoutTextViewID);

myText.setTextColor(getResources().getColor(R.color.YOURCOLOR);

OR

myText.setTextColor(Color.parseColor("#54D66A"));
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1
holder.userType.setTextColor(context.getResources().getColor(
                    R.color.green));
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43
1

Try this:

textView.setTextColor(getResources().getColor(R.color.errorColor, null));
mohamad sheikhi
  • 394
  • 5
  • 16
  • 3
    it was mentioned above, but it bears repeating here: getColor is deprecated as of android M. It may disappear some day. – John Lord Nov 20 '19 at 17:22
1

Try this:

TextView textview = (TextView) findViewById(R.id.textview );
textview .setTextColor(Color.parseColor("#85F85F"));
mohamad sheikhi
  • 394
  • 5
  • 16
0

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"));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Raj Sharma
  • 141
  • 1
  • 4
0

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

Moak
  • 12,596
  • 27
  • 111
  • 166
Comrade
  • 119
  • 11
0

You can use textView.setTextColor(Color.BLACK) to use any of the in-built colors of the Color class.

You can also use textView.setTextColor(Color.parseColor(hexRGBvalue)) to define custom colors.

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
shravs
  • 51
  • 1
  • 2
0

If you are in an adapter and still want to use a color defined in resources you can try the following approach:

holder.text.setTextColor(holder.text.getContext().getResources().getColor(R.color.myRed));
Aleks Nine
  • 249
  • 7
  • 15
0
TextView textresult = (TextView)findViewById(R.id.textView1);
textresult.setTextColor(Color.GREEN);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

getColor() is depreceted

So try this way:

 tv_title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sf_white));
kgandroid
  • 5,507
  • 5
  • 39
  • 69
0

I was doing this for a TextView in a ViewHolder for a RecyclerView. I'm not so sure why, but it didn't work for me in the ViewHolder initialization.

public ViewHolder(View itemView) {
    super(itemView);
    textView = (TextView) itemView.findViewById(R.id.text_view);
    textView.setTextColor(context.getResources().getColor(R.color.myColor));
    // Other stuff
}

But when I moved it to the onBindViewHolder, it worked fine.

public void onBindViewHolder(ViewHolder holder, int position){
    // Other stuff
    holder.textView.setTextColor(context.getResources().getColor(R.color.myColor));
}

Hope this helps someone.

IsaiahJ
  • 454
  • 7
  • 19
0
TextView color= (TextView)findViewById(R.id.color);
text.setTextColor(Color.RED);
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 
    b.numberDay1.setTextColor(ContextCompat.getColor(requireContext(), R.color.secondary_100))
} else {                
    b.numberDay1.setTextColor(resources.getColor(R.color.secondary_100))
}
Fortran
  • 2,218
  • 2
  • 27
  • 33
0

This worked for me inside of recycler View adapter (Compile API = 33):

@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
.
.
.

holder.view.myTextView.setTextColor(holder.itemView.getResources().getColor(R.color.green));
.
.
.
}
C.F.G
  • 817
  • 8
  • 16
0

I know there are many questions and it's late to answer this now but just to help someone who is stuck like me:

If you get an error Cannot resolve symbol Color

You have to import android.graphics.Color and then txt.setTextColor(Color.parseColor("#FFFFFF"))

44yu5h
  • 111
  • 5
-1

Try using the following code :

holder.text.setTextColor(Color.parseColor("F00"));
Tharif
  • 13,794
  • 9
  • 55
  • 77