android – Animations and setVisibility-ThrowExceptions

Exception or error:

I have a LinearLayout that I want to be able to show/hide by clicking on a “more details” link. I do this by calling

moreDetailsSection.setVisibility(View.VISIBLE);

or

moreDetailsSection.setVisibility(View.GONE);

to show/hide it. This works fine, but I wanted to add an animation that makes the layout fields slide in nicely, but this is only run the first time the field is made visible, if I hide it and show it again the field simply appears all of a sudden. Here is the animation code (moreDetailsSection is the Layout in question):

      AnimationSet set = new AnimationSet(true);

      Animation animation = new AlphaAnimation(0.0f, 1.0f);
      animation.setDuration(250);
      set.addAnimation(animation);

      animation = new TranslateAnimation(
          Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
          Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
      );
      animation.setDuration(150);
      set.addAnimation(animation);

      LayoutAnimationController controller =
          new LayoutAnimationController(set, 0.25f);
      moreDetailsSection.setLayoutAnimation(controller);

Any advice on how to make this run EACH time I show the layout and not only the first time?

How to solve:

I assume that the moreDetailsSection is INVISIBLE at first.
you just have to create the Animation object and call the following code when the more details link is clicked.

moreDetailsSection.startAnimation(animation);
moreDetailsSection.setVisibility(View.VISIBLE);

###

You can use this line in your view for that layout:

android:animateLayoutChanges="true"

Leave a Reply

Your email address will not be published. Required fields are marked *