How to make text fade in and out in Android?

You should consider using something like a TextSwitcher. There is a brief document on TextSwitcher in the Android documentation. What I would recommend best though is to look at the API Demos, there is a great and simple to use one of TextSwitchers. Download the API Demos and check them out for yourself, or see here.


You seem to be setting the animation to in right after you had set it to out. This makes only the "in" animation work.

To make the second animation start right after the first, you can add a listener to your first animation:

out.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationEnd(Animation animation) {
        mSwitcher.setText("New Text");
        mSwitcher.startAnimation(in);

    }
});

Then, in your onClick() method:

public void onClick(View v) {

    mSwitcher.startAnimation(out);

}

That should do the trick.


Another approach is to use AnimationSet.

final Animation in = new AlphaAnimation(0.0f, 1.0f);
in.setDuration(3000);

final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(3000);

AnimationSet as = new AnimationSet(true);
as.addAnimation(out);
in.setStartOffset(3000);
as.addAnimation(in);

Then, instead of starting out, start as.

I hope this helps!


If you want to use Animation, you can use an AnimatorListener to listen for when the first animation is done, and then start the second animation. That would be onAnimationEnd().

More information available here: http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html

There might be a better way to do it with AnimationSet, but this works for sure.