2

I have in my styles

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MYTheme" parent="...">
        <item name="android:navigationBarColor">@color/navigationBarColor</item>
    </style>
</resources>

I know I can change the color of the navigation bar with

getWindow().setNavigationBarColor(...);

I want change the color with an animation, a transition between the current color and the new color

Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
rkmax
  • 17,633
  • 23
  • 91
  • 176

2 Answers2

6

You can animate color change using ValueAnimator.ofArgb. I must mention that it is supported only beginning with API >= 21. This shouldn't be a problem, though, because setNavigationBarColor is >= 21 too.

int from = getWindow().getNavigationBarColor();
int to = Color.BLACK; // new color to animate to

ValueAnimator colorAnimation = ValueAnimator.ofArgb(from, to);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        getWindow().setNavigationBarColor((Integer) animator.getAnimatedValue());
    }
});
colorAnimation.start();
Egor Neliuba
  • 14,784
  • 7
  • 59
  • 77
0

I agree partially with Egor N's answer. But my answer will make your app compatible backwards to API 11 instead of 21, as he suggests.

I share this answer

Integer colorFrom = getWindow().getNavigationBarColor();
Integer colorTo = Color.BLACK;
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {

    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        getWindow().setNavigationBarColor((Integer) animator.getAnimatedValue());
    }

});
colorAnimation.start();
Community
  • 1
  • 1
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64