MichelReap MichelReap - 2 years ago 1473
Android Question

Show and hide a View with a slide up/down animation

I have a

that I want to show or hide with an
that pushes the layout upwards or downwards whenever I change its visibility.

I've seen a few samples out there but none of them suit my needs.

I have created two xml files for the animations but I do not know how to start them when I change the visibility of a

Answer Source

With the new animation API that was introduced in Android 3.0 (Honeycomb) it is very simple to create such animations.

Sliding a View down by a distance:


You can later slide the View back to its original position like this:


You can also easily combine multiple animations. The following animation will slide a View down by its height and fade it in at the same time:

// Prepare the View for the animation

// Start the animation

You can then fade the View back out and slide it back to its original position. We also set an AnimatorListener so we can set the visibility of the View back to GONE once the animation is finished:

    .setListener(new AnimatorListenerAdapter() {
        public void onAnimationEnd(Animator animation) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download