Android fade in and fade out animation programatically

Fade in animation. Pass in a view such as LinearLayout, RelativeLayout, TextView or any other views and a duration which specifies how long should the animation lasts.

public static void fadeInAnimation(final View view, long animationDuration) {
    Animation fadeIn = new AlphaAnimation(0, 1);
    fadeIn.setInterpolator(new DecelerateInterpolator());
    fadeIn.setDuration(animationDuration);
    fadeIn.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }
        @Override
        public void onAnimationEnd(Animation animation) {
            view.setVisibility(View.VISIBLE);
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });

    view.startAnimation(fadeIn);
}

Fade out animation.

public static void fadeOutAnimation(final View view, long animationDuration) {
    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new AccelerateInterpolator());
    fadeOut.setStartOffset(animationDuration);
    fadeOut.setDuration(animationDuration);
    fadeOut.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }
        @Override
        public void onAnimationEnd(Animation animation) {
            view.setVisibility(View.INVISIBLE);
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    });

    view.startAnimation(fadeOut);
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search