Search code examples
javaandroidlayoutfade

Fade effect between layouts


As by object, I would reproduce fade effect between two layout. Now I've this situation:

LinearLayout l;
LinearLayout l2;

To switch between them I've used

l.setVisibility(View.GONE);
l2.setVisibility(View.VISIBLE);

I want add fade effect between this transiction, how I can do it?


Solution

  • Using R.anim.fade_out & .R.anim.fade_in you can create an animation which does this. I don't know much about this myself but heres a tutorial regarding animations in android: Animation Tutorial

    P.S. This tutorial is not mine thus credit does not go out to me.

    Edit:

    AnimationSet set = new AnimationSet(true);
    
    Animation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(50);
    set.addAnimation(animation);
    
    animation = new TranslateAnimation(
    Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
    Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
    );
    animation.setDuration(100);
    set.addAnimation(animation);
    
    LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
    l.setLayoutAnimation(controller);
    

    Fade out Animation

    public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
      Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
      target.startAnimation(animation);
      return animation;
    }
    

    I'm guessing you can try something like this, I copy pasted the animation from the tutorial I don't know what it does exactly as I have no experience with Android development. Another example could be Example 2