Android: show/hide a view using an animation Android: show/hide a view using an animation android android

Android: show/hide a view using an animation


Set the attribute android:animateLayoutChanges="true"inside the parent layout .

Put the view in a layout if its not and set android:animateLayoutChanges="true" for that layout.

NOTE: This works only from API Level 11+ (Android 3.0)


I created an extension for RelativeLayout that shows/hides Layouts with animations.It can extend any kind of View to gain these features.

import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.widget.RelativeLayout;public class AnimatingRelativeLayout extends RelativeLayout{    Context context;    Animation inAnimation;    Animation outAnimation;    public AnimatingRelativeLayout(Context context)    {        super(context);        this.context = context;        initAnimations();    }    public AnimatingRelativeLayout(Context context, AttributeSet attrs)    {        super(context, attrs);        this.context = context;        initAnimations();    }    public AnimatingRelativeLayout(Context context, AttributeSet attrs, int defStyle)    {        super(context, attrs, defStyle);        this.context = context;        initAnimations();    }    private void initAnimations()    {        inAnimation = (AnimationSet) AnimationUtils.loadAnimation(context, R.anim.in_animation);        outAnimation = (Animation) AnimationUtils.loadAnimation(context, R.anim.out_animation);    }    public void show()    {        if (isVisible()) return;        show(true);    }    public void show(boolean withAnimation)    {        if (withAnimation) this.startAnimation(inAnimation);        this.setVisibility(View.VISIBLE);    }    public void hide()    {        if (!isVisible()) return;        hide(true);    }    public void hide(boolean withAnimation)    {        if (withAnimation) this.startAnimation(outAnimation);        this.setVisibility(View.GONE);    }    public boolean isVisible()    {        return (this.getVisibility() == View.VISIBLE);    }    public void overrideDefaultInAnimation(Animation inAnimation)    {        this.inAnimation = inAnimation;    }    public void overrideDefaultOutAnimation(Animation outAnimation)    {        this.outAnimation = outAnimation;    }}

You can override the original Animations using overrideDefaultInAnimation and overrideDefaultOutAnimation

My original Animations were fadeIn/Out, I am adding XML animation files for Translating in/out of the screen (Translate to top and from top)

in_animation.xml:

    <?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="600"    android:fillAfter="false"    android:fromXDelta="0"    android:fromYDelta="-100%p"    android:toXDelta="0"    android:toYDelta="0" />

out_animation.xml:

  <?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="600"    android:fillAfter="false"    android:fromXDelta="0"    android:fromYDelta="0"    android:toXDelta="0"    android:toYDelta="-100%p" />


This can reasonably be achieved in a single line statement in API 12 and above. Below is an example where v is the view you wish to animate;

v.animate().translationXBy(-1000).start();

This will slide the View in question off to the left by 1000px. To slide the view back onto the UI we can simply do the following.

v.animate().translationXBy(1000).start();

I hope someone finds this useful.