Animate change of view background color on Android


How do you animate the change of background color of a view on Android?

For example:

I have a view with a red background color. The background color of the view changes to blue. How can I do a smooth transition between colors?

If this can't be done with views, an alternative will be welcome.

1/21/2017 10:28:00 AM

Accepted Answer

I ended up figuring out a (pretty good) solution for this problem!

You can use a TransitionDrawable to accomplish this. For example, in an XML file in the drawable folder you could write something like:

<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="">
    <!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
    <item android:drawable="@drawable/original_state" />
    <item android:drawable="@drawable/new_state" />

Then, in your XML for the actual View you would reference this TransitionDrawable in the android:background attribute.

At this point you can initiate the transition in your code on-command by doing:

TransitionDrawable transition = (TransitionDrawable) viewObj.getBackground();

Or run the transition in reverse by calling:


See Roman's answer for another solution using the Property Animation API, which wasn't available at the time this answer was originally posted.

5/23/2017 12:26:35 PM

You can use new Property Animation Api for color animation:

int colorFrom = getResources().getColor(;
int colorTo = getResources().getColor(;
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setDuration(250); // milliseconds
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

    public void onAnimationUpdate(ValueAnimator animator) {
        textView.setBackgroundColor((int) animator.getAnimatedValue());


For backward compatibility with Android 2.x use Nine Old Androids library from Jake Wharton.

The getColor method was deprecated in Android M, so you have two choices:

  • If you use the support library, you need to replace the getColor calls with:

  • if you don't use the support library, you need to replace the getColor calls with:


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow