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?
Crossfade the views For the view that is fading in, set the alpha value to 0 and the visibility to VISIBLE . (Remember that it was initially set to GONE .) This makes the view visible but completely transparent. For the view that is fading in, animate its alpha value from 0 to 1 .
Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.
Crossfade animations (also know as dissolve) gradually fade out one UI component while simultaneously fading in another. This animation is useful for situations where you want to switch content or views in your app. Crossfades are very subtle and short but offer a fluid transition from one screen to the next.
Android offers pre-loaded animation that the system runs each time you make a change to the layout. All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With