Phil Ringsmuth
Phil Ringsmuth

Reputation: 2037

Two-part transition animation in Android: Slide a textview out to the left, and bring another in from the right

I am trying to set up a simple animation in an Android activity where when the user swipes over an area from right to left, a textview at the top of the activity will slide off the screen to the left, then it will slide in from the right with new text.

Before:
---------------
- First Text  -
---------------

Animation Part 1:
---------------
-st Text      -
---------------

Animation Part 2:
---------------
-     Second T-
---------------

After:
---------------
- Second Text -
---------------

When I try to use two separate animations, and change the text in between, the second animation always overrides the first one and the first one is never seen. Even adding a StartOffset to the second animation will not work.

Any suggestions would be helpful. Thank you.

Upvotes: 2

Views: 3065

Answers (2)

RAnderson
RAnderson

Reputation: 190

I find the simplest way to do this is using AnimationListeners. Set the Animation Listener on the initial animation. Then, use the onAnimationEnd to start your second animation. Android doesn't allow you to start another animation directly from onAnimationEnd, so you will need to use a handler.

Here is a basic outline of what I usually use.

@Override
public void onAnimationEnd(Animation animation) {
    Handler curHandler = new Handler();
    curHandler.post(launchSecondAnimation);
}

private Runnable launchSecondAnimation = new Runnable() {
    public void run() {
        // Change the text of the textbox and start the second animation
    }
};

Upvotes: 1

Joel Martinez
Joel Martinez

Reputation: 47749

Why not simplify things and just use two textviews? just set the "offscreen" textview with the secondary text and it will nicely scroll into view without having to worry about the sleight of hand you're working on now :-)

Upvotes: 2

Related Questions