Alex Orlov Alex Orlov - 1 year ago 108
Android Question

Text color animation

Is there a way to animate a text color change (from anycolor to white)?

The only variant I came up with, is placing two textviews (with the same text) in one place, and fading the top one, so the bottom one (that has a white color) will become visible.

P.S. I scrapped the variant of the 2 TextViews since it looked weird (edges weren't smooth and, since I have a lot of such elements on the screen it was really lagging the scrolling). What I did, was a crazy hack that does the animation with the use of a Thread and setTextColor (that also forces redraw of a textview).

Since I needed only 2 color changes (from red to white, and from green to white) I hardcoded the values and all of the transition colors between them. So here's how it looks:

public class BlinkingTextView extends TextView {
public BlinkingTextView(Context context, AttributeSet attrs) {
super(context, attrs);

public void animateBlink(final boolean red) {
if (animator != null) {
animator = new Animator(this, red);

public void clearBlinkAnimation() {
if (animator != null) {

private Animator animator;

private final static class Animator extends Thread {
public Animator(final TextView textView, final boolean red) {
this.textView = textView;
if (red) {
} else {

private TextView textView;

private final int[] SET_TO_USE;

private final static int[] RED = {
private final static int[] GREEN = {

private boolean stop;

public void run() {
int i = 0;
while (i < 15) {
if (stop) break;
final int color = SET_TO_USE[i];
if (stop) break; Runnable() {
public void run() {
if (!stop) {
if (stop) break;
if (stop) break;
try {
} catch (InterruptedException e) {}
if (stop) break;

public void drop() {
stop = true;

Answer Source

Although I haven't found a totally distinct method, I have tried to use a TextSwitcher (with the fade animation) to create the colour-change effect. A TextSwitcher is a kind of ViewSwitcher which literally animates between two (internal) TextViews. Did you manually implement the same system unknowingly? ;) It manages a bit more of the process for you, so you may find it easier to work with (especially if you want to try more involved animations). I would create new subclass of TextSwitcher and some methods e.g. setColour() which can set the new colour and then trigger an animation. The animation code can then be moved outside of your main application.

  • make sure you keep a handle on the two TextViews that are put into the switcher
  • change the colour of the other TextView and call setText() to animate between them

If you are already using a ViewSwitcher then I don't think there is an easier way to implement this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download