TextView setTextColor() not working

AndroidColorsTextview

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

textView.setTextColor(Color.RED);

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
Questiondidi_X8View Question on Stackoverflow
Solution 1 - AndroidSunil Kumar SahooView Answer on Stackoverflow
Solution 2 - AndroiddugguView Answer on Stackoverflow
Solution 3 - Androidsneha vView Answer on Stackoverflow
Solution 4 - AndroidChad MxView Answer on Stackoverflow
Solution 5 - AndroidAbhiView Answer on Stackoverflow
Solution 6 - AndroidSayan MazumderView Answer on Stackoverflow