Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate Layout Orientation change in Android

Hoping to find out if it's possible to animate a layout orientation change. I am programmatically changing an LinearLayout orientation which contains a few views from vertical to horizontal and I don't think applying android:animateLayoutChanges="true" to the layout will automatically animate the change when the views move from being on top of each other to side by side. Is there any way to animate these changes using linearLayout.setOrientation(LinearLayout.HORIZONTAL) rather than something like an ObjectAnimator which causes more issues than it's worth in this situation? Thanks

like image 823
Jaz Avatar asked Apr 15 '26 13:04

Jaz


1 Answers

Yes, you need to add this to your LinearLayout section as you mentioned.

android:animateLayoutChanges="true"

The default behavior for "animateLayoutChanges" will not animate LinearLayout orientation changes. To get that behavior you need to enable it with this:

val linearLayout = view.findViewById<LinearLayout>(R.id.my_linear_layout)
linearLayout.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)

The details can be found here LayoutTransition Documentation, but the important piece is this:

That is, all transition types besides CHANGING are enabled by default. You can also enable CHANGING animations by calling this method with the CHANGING transitionType.

like image 193
Tom Avatar answered Apr 18 '26 02:04

Tom