Animating views on Android

Note to self: whenever you try to animate a view that is either GONE or has height 0 this will not work the first time. The moment the user touches the screen or an UI update is triggered only then will the animation start.

Of course I found this Stack Overflow question about not starting animations, but the view was defined as being VISIBLE. I also explicitly made the view visible in code – just to be sure – but this was not enough. One response suggested setting the height to something larger than 0. I gave this some thought, but instead of manipulating the height I put in

view.setMinimumHeight(1)
view.requestLayout

After some crazy hours investigating threads, creating many Handlers and Runnables I figured to give this height-thingy an extra try. And it worked, finally!

So, whenever you animate the height of a view starting from 0, use:

Animation animation = new SomeAnimation(view, 500, height);
view.getLayoutParams().height = 1;
view.requestLayout();
view.startAnimation(animation);