0

Ok, so I have worked the fading out content from within an id (thanks to https://stackoverflow.com/a/4570977/971392).

I have the following function:

public void switchView(View view) {
        LayoutInflater inflater = LayoutInflater.from(getBaseContext());
        View to_load = inflater.inflate(R.layout.activity_register, null, false); 

        RelativeLayout rl = (RelativeLayout) findViewById(R.id.content_login);
        Animation fadeOutAnim = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.fadeout);
        rl.startAnimation(fadeOutAnim);
        //rl.setVisibility(View.GONE);
        //rl.removeView(view);
        rl.addView(to_load);
}

It does the desired action, IF I comment the rl.startAnimation(fadeOutAnim) which is there only for the purpose of giving an animation on change view.

With the animation executing, the function changes the view through the rl.addView() but then applies the fadeOut and everything disappears.

So, how may I use the fadeout, without it removing my content?

Community
  • 1
  • 1
Alex
  • 7,538
  • 23
  • 84
  • 152

1 Answers1

1

you need to delay the rl.addView(to_load); call until after the fade out animation is complete. The way to do that is with an Animation Listener.

Try like this:

AnimationListener animListen = new AnimationListener() {
    public void onAnimationEnd(Animation animation) {
        rl.addView(to_load);
    }

    public void onAnimationRepeat(Animation animation) {
    }

    public void onAnimationStart(Animation animation) {
    }
});

Animation fadeOutAnim = AnimationUtils.loadAnimation(LoginActivity.this, R.anim.fadeout);
fadeOutAnim.setAnimationListener(animListen);
rl.startAnimation(fadeOutAnim);
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156