82

I programmatically create a list (no a ListView, just adding them to the parent) of such elements:

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="vertical" android:layout_weight="1">
    <TextView android:id="@+id/filiale_name"
    android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/lagerstand_text"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:textSize="10sp" android:textColor="@color/red"/>
</LinearLayout>

Also, I have defined some colors in values/colors.xml. As you see, the TextView with id "lagerstand_text" has set it's color to red by default. That works.

When creating the elements in Java, I do

lagerstandText.setText("bla");

and for some elements also I do

lagerstandText.setTextColor(R.color.red);

and other colors. While the elements on which I don't call setTextColor() are red, all others are grey, no matter which color I chose (even if it's the same red again).

Why is that?

didi_X8
  • 5,018
  • 10
  • 42
  • 46

6 Answers6

236

The documentation is not very verbose about this, but you cannot use just the R.color integer when calling setTextColor. You need to call getResources().getColor(R.color.YOURCOLOR) to set a color properly.

Use the following to set color of your text programmatically:

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

Starting with the support library 23 you have to use the following code, because getColor is deprecated:

textView.setTextColor(ContextCompat.getColor(context, R.color.YOURCOLOR));
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • 2
    Ok, that works. The API documentation could be a bit more verbose in this case... – didi_X8 May 30 '11 at 14:31
  • 13
    also u can use Color. ( here red green black blue yellow and other) setTextColor(Color.RED) – Peter May 30 '11 at 14:51
  • Thanks for the info... but this getResources() makes me pass a chain of contexts. There should be some better way to accessing global resources. – Umair Sep 14 '12 at 07:25
  • @Umair Use a static variable on your Application class. – Arif Amirani Apr 26 '13 at 21:23
  • @Kontinuity, I read that keeping the context in static variable is big source of memory leakage. – Umair Apr 28 '13 at 18:04
  • @Umair http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android – Arif Amirani Apr 29 '13 at 04:35
  • Its working for me if i use R.color.green. But not working if i use android.R.color.white. Any idea why? – abhijit.mitkar Dec 04 '14 at 15:29
  • getColor(R.color.YOURCOLOR) has been deprecated in v23. See this [link](http://stackoverflow.com/questions/31590714/getcolorint-id-deprecated-on-android-6-0-marshmallow-api-23) for the new way – themichaelscott Jan 14 '16 at 08:29
35

So, there are many ways to achieve this task.

1.

int color = Integer.parseInt("bdbdbd", 16)+0xFF000000;
textview.setTextColor(color);

2.

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

3.

textView.setTextColor(0xffbdbdbd);

4.

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

5.

textView.setTextColor(Color.argb(a_int, r_int, g_int, b_int));
tomDev
  • 5,620
  • 5
  • 29
  • 39
duggu
  • 37,851
  • 12
  • 116
  • 113
  • Is there any way to find out if a particular color value is going to make the text disappear? – Christopher Masser Dec 02 '13 at 12:50
  • I'm adjusting the brightness of the color before using setTextColor(color). In some unknown cases the TextView simply disappears on the device (independent of the background). I want to write a test function to check if "color" is a valid color value before using it in setTextColor(color). – Christopher Masser Dec 02 '13 at 15:27
  • @ChristopherMasser not trying anything what u said ?? – duggu Dec 03 '13 at 04:24
4

1.standard color u prefer please go with below .

textview.setTextColor(Color.select_color)

2.here want to use custwom color add it in color.xml file

textview.setTextColor(getResources().getColor(R.color.textbody));

or

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

or

subText.setTextColor(Color.rgb(255,192,0));
sneha v
  • 41
  • 6
2

For future reference, you can use the follow:

String color = getString(Integer.parseInt(String.valueOf(R.color.my_color)));
my_textView.setTextColor(Color.parseColor(color));

This way you can make use of your Color Resources.

Chad Mx
  • 1,266
  • 14
  • 17
1
textView.setTextColor(Color.RED);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

The integer id for a particular color(defined in xml layout) defined in R class cannot be passed as a parameter to setTextColor() method of View class. You must obtain the parameter of the setTextColor() by the following line of code :

int para=getResources().getColor(R.color.your_color,null);
view.setTextColor(para,null);

The method getColor(int id) has been depreciated...instead use getColor(int id,Resources.Theme theme) as in the line of code above.

The `second parameter( theme )` can be null
Abhi
  • 116
  • 7