How to Set Text Color of a Textview Programmatically

How to set text color of a TextView programmatically?

Use,..

Color.parseColor("#bdbdbd");

like,

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

Or if you have defined color code in resource's color.xml file than

(From API >= 23)

mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));

(For API < 23)

mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));

How to set the text color of TextView in code?

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.

How to change text color programmatically in kotlin

You can use:

   //with a color  
myText.setTextColor(ContextCompat.getColor(this,R.color.orange_or))
//with a selector
myText.setTextColor(AppCompatResources.getColorStateList(this, R.color.xxx))

Android: Set textcolor for programmatically created TextView

According to your xml file you need to change

txtNumber.setTextColor(getResources().getColor(R.color.blue));

to

txtNumber.setTextColor(getResources().getString(R.color.blue));

Further more you can make color.xml file in your values folder and in that use

<color name="mycolor">#33CCCC</color>

now just use this way

txtNumber.setTextColor(getResources().getColor(R.color.mycolor));

Programmatically set text color to primary android textview

Finally I used the following code to get the primary text color of the theme -

// Get the primary text color of the theme
TypedValue typedValue = new TypedValue();
Resources.Theme theme = getActivity().getTheme();
theme.resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
TypedArray arr =
getActivity().obtainStyledAttributes(typedValue.data, new int[]{
android.R.attr.textColorPrimary});
int primaryColor = arr.getColor(0, -1);

How to programmatically change the colour of a textView, to a colour saved in an attributes file?

First define Color you want to set on Textview via color.xml file, for example ->

<color name="colorPrimary">#3F51B5</color>

Then inside your java file, This single line will set color on textview, note that the color will be fetched from defined colors inside color.xml file. ->

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

Android change text color automatically programmatically

You can try this

In Kotlin

val handler = Handler()
val colors = arrayOf(Color.BLUE, Color.WHITE, Color.YELLOW, Color.GREEN)
var i;
val runnable = Runnable {
i = i % colors.size
yourView.setTextColor(colors[i])
i++
handler.postDelayed(this, 1000)
}
handler.postDelayed(runnable, 1000)

or in Java

 Handler handler = new Handler();
int[] colors = {Color.BLUE, Color.WHITE, Color.YELLOW, Color.GREEN};
int i;
Runnable runnable = new Runnable () {
@Override
public void run() {
i = i % colors.length;
yourView.setTextColor(colors[i]);
i++;
handler.postDelayed(this, 1000);
}
}
handler.postDelayed(runnable, 1000);

Setting Android TextView text color programmatically with setTextColor() renders android:duplicateParentState invalid

OK, found my mistake. I should be using getResources().getColorStateList() instead of getResources().getColor(). Leaving this here for anyone else who makes the same mistake.



Related Topics



Leave a reply



Submit