How to set the text color of TextView in code?

AndroidColorsTextview

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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));

Solution 3 - Android

And another one:

TextView text = (TextView) findViewById(R.id.text);
text.setTextColor(Color.parseColor("#FFFFFF"));

Solution 4 - Android

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));

Solution 5 - Android

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

Solution 6 - Android

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" />

Solution 7 - Android

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));
    

Solution 8 - Android

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).

Solution 9 - Android

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>

Solution 10 - Android

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.

Solution 11 - Android

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.

Solution 12 - Android

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

Solution 13 - Android

Use:

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

Solution 14 - Android

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

or

myTextView.setTextColor(0xAARRGGBB);

Solution 15 - Android

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)
}

Solution 16 - Android

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"));

Solution 17 - Android

text1.setTextColor(Color.parseColor("#000000"));

Solution 18 - Android

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.

Solution 19 - Android

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))

Solution 20 - Android

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

Use this instead:

textView.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.color_black));

Solution 21 - Android

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"));

Solution 22 - Android

   textViewStatus.setTextColor(res.getColor(R.color.green));

Solution 23 - Android

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);

Solution 24 - Android

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.

Solution 25 - Android

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"));

Solution 26 - Android

holder.userType.setTextColor(context.getResources().getColor(
					R.color.green));

Solution 27 - Android

Try this:

TextView textview = (TextView) findViewById(R.id.textview );
textview .setTextColor(Color.parseColor("#85F85F"));

Solution 28 - Android

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"));

Solution 29 - Android

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

Solution 30 - Android

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.

Solution 31 - Android

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));

Solution 32 - Android

TextView textresult = (TextView)findViewById(R.id.textView1);
textresult.setTextColor(Color.GREEN);

Solution 33 - Android

getColor() is depreceted

So try this way:

 tv_title.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.sf_white));

Solution 34 - Android

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.

Solution 35 - Android

Try this:

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

Solution 36 - Android

TextView color= (TextView)findViewById(R.id.color);
text.setTextColor(Color.RED);

Solution 37 - Android

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))
}

Solution 38 - Android

Try using the following code :

holder.text.setTextColor(Color.parseColor("F00"));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVikasView Question on Stackoverflow
Solution 1 - AndroidNanneView Answer on Stackoverflow
Solution 2 - AndroidxbakesxView Answer on Stackoverflow
Solution 3 - AndroidLEXView Answer on Stackoverflow
Solution 4 - AndroidnewdayView Answer on Stackoverflow
Solution 5 - AndroidGanapathy CView Answer on Stackoverflow
Solution 6 - AndroidChinnaView Answer on Stackoverflow
Solution 7 - AndroidRankView Answer on Stackoverflow
Solution 8 - AndroidA.WView Answer on Stackoverflow
Solution 9 - AndroidKaushik ChatterjeeView Answer on Stackoverflow
Solution 10 - AndroidRobin GawendaView Answer on Stackoverflow
Solution 11 - AndroidArnab ChakrabortyView Answer on Stackoverflow
Solution 12 - AndroidNitishView Answer on Stackoverflow
Solution 13 - AndroidTalhaView Answer on Stackoverflow
Solution 14 - AndroidYash PatilView Answer on Stackoverflow
Solution 15 - AndroidGiboltView Answer on Stackoverflow
Solution 16 - AndroidHiren PatelView Answer on Stackoverflow
Solution 17 - AndroidRavina BhavsarView Answer on Stackoverflow
Solution 18 - AndroidEvon TechnologyView Answer on Stackoverflow
Solution 19 - AndroidMoriView Answer on Stackoverflow
Solution 20 - AndroidPJ2104View Answer on Stackoverflow
Solution 21 - Androiduser2285778View Answer on Stackoverflow
Solution 22 - Androidzudo1337View Answer on Stackoverflow
Solution 23 - AndroidreshmaView Answer on Stackoverflow
Solution 24 - AndroidCristiano GuerraView Answer on Stackoverflow
Solution 25 - AndroidIntelliJ AmiyaView Answer on Stackoverflow
Solution 26 - AndroidHarish GyananiView Answer on Stackoverflow
Solution 27 - Androidmohamad sheikhiView Answer on Stackoverflow
Solution 28 - AndroidRaj SharmaView Answer on Stackoverflow
Solution 29 - AndroidComradeView Answer on Stackoverflow
Solution 30 - AndroidshravsView Answer on Stackoverflow
Solution 31 - AndroidAleks NineView Answer on Stackoverflow
Solution 32 - AndroidPedro LobitoView Answer on Stackoverflow
Solution 33 - AndroidkgandroidView Answer on Stackoverflow
Solution 34 - AndroidIsaiahJView Answer on Stackoverflow
Solution 35 - Androidmohamad sheikhiView Answer on Stackoverflow
Solution 36 - AndroidRaihan MahamudView Answer on Stackoverflow
Solution 37 - AndroidFortranView Answer on Stackoverflow
Solution 38 - AndroidSumit GargView Answer on Stackoverflow