I have a LinearLayout
, for which I apply android:animateLayoutChanges="true"
in the parent LinearLayout
. When the user clicks a toggle button, the LinearLayout
"collapses" (I set the view's visibility as LinearLayout.GONE
programmatically, and when they click it again, it expands by programmatically setting the visibility back to LinearLayout.VISIBLE
.
The animation of it collapsing and expanding works correctly.
However, any items below the collapsable/expandable LinearLayout
snap back to the top before the animation of the collapse is complete. The items that are snapping back are NOT inside the parent which has animateLayoutChanges
set to true
, and I don't think there is any way I can put them inside it.
Here is my layout hierarchy (I didn't mention the attributes to keep it short):
<!-- Top most LinearLayout-->
<LinearLayout>
<!-- LinearLayout containing android:animateLayoutChanges="true"-->
<LinearLayout>
<!-- RelativeLayout containing button to toggle LinearLayout visibility below-->
<RelativeLayout>
</RelativeLayout>
<!-- LinearLayout that has its visibility toggled -->
<LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
This entire layout is inserted programmatically into another LinearLayout
(see below):
<LinearLayout
android:id="@+id/form_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- This is where the previous XML layout containing the toggle-able LinearLayout
is inserted programmatically. -->
<!-- This button snaps back up to the top before the animation is complete. -->
<Button />
</LinearLayout>
I realize the problem would be solved if I added the Button
that snaps up to the LinearLayout
that has animateLayoutChanges
as true. However, this isn't an options for a few reasons.
So is there any other work around?
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. Tip: If you want to supply custom layout animations, create a LayoutTransition object and supply it to the layout with the setLayoutTransition () method.
Recently, I have discovered a new one regarding android:animateLayoutChanges. For those unfamiliar with this XML attribute, it’s a “ automagical ” way to animate changes in your ViewGroups. This has been around since API 11 aka Honeycomb.
Tip: If you want to supply custom layout animations, create a LayoutTransition object and supply it to the layout with the setLayoutTransition () method. If you want to jump ahead and see a full working example, download and run the sample app and select the Crossfade example. See the following files for the code implementation:
In your activity's layout XML file, set the android:animateLayoutChanges attribute to true for the layout that you want to enable animations for. For instance:
Instead of using animateLayoutChanges
try adding
TransitionManager.beginDelayedTransition(transitionsContainer);
in your onClick method where transitionsContainer
is parent of views that should be animated.
For example your parent layout is is
<LinearLayout android:id="@+id/transitions_container">
<!--add your widgets here-->
</LinearLayout>
And in code
final ViewGroup transitionsContainer = (ViewGroup) view.findViewById(R.id.transitions_container);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.beginDelayedTransition(transitionsContainer);
// do your staff with changing children of transitionsContainer
}
});
Check https://medium.com/@andkulikov/animate-all-the-things-transitions-in-android-914af5477d50 for details.
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