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:

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?

 Answers

42

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.

Tuesday, June 1, 2021
 
samayo
answered 6 Months ago
20

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():
    text.set_color("red")
Monday, June 14, 2021
 
Lance
answered 6 Months ago
25

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

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

Friday, August 13, 2021
 
c0m4
answered 4 Months ago
38

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
 
McDowell
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 :  
Share