Asked  6 Months ago    Answers:  5   Viewed   36 times

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:


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?



You should use:


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

  • Color.parseColor (Manual) (like LEX uses)

  • Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses)

  • 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:


    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:


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.

Tuesday, June 1, 2021
answered 6 Months ago

call Legend.get_texts() will get a list of Text object in the legend object:

import pylab as pl
pl.plot(randn(100), label="randn")
l = legend()
for text in l.get_texts():
Monday, June 14, 2021
answered 6 Months ago

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:


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));
Saturday, July 31, 2021
Don Stanislas
answered 4 Months ago

Specify android:duplicateParentState='true' on your TextView.

Friday, August 13, 2021
answered 4 Months ago

Note: At the time I wrote this answer I was working on iOS 7 & this workaround worked on iOS 8 as well. It may not work on iOS 9.

Ok it's been two days since I posted this question. Though I din't get the answer I got a workaround for this problem.

Note : I am using storyboard for my application & I have subclassed the UISearchBar and this workaround working like a gem for me.

First and foremost add an appearance proxy to UILabel when its contained in the UISearch bar class like this :

[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];//change the color to whichever color needed

Then the most important thing is If you're using storyboard you must and should write some text in the placeholder field of the attribute inspector like this

If you forget to write something in the placeholder field, definitely this trick will not work when you run the application and the placeholder text will look in usual gray color.

Once you're done with writing some text in placeholder field, set the place holder text in your UISearchBar sub class like this :

self.placeholder = @"My Placeholder text";

Now run the application !!! You'll get the place holder text in the color which you have set in appdelegate:) Hope this helps someone :)

Saturday, November 27, 2021
answered 2 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :