The little secret of android:animateLayoutChanges

Benjamin Monjoie
ProAndroidDev
Published in
3 min readJun 29, 2017

--

Every now and then, Android reveals some of its secrets to us and that is what makes Android development so fun. 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.

A quick look at the documentation shows us that it’s actually using a LayoutTransition object which can be set on any ViewGroup using its method setLayoutTransition . Of course, if the attribute android:animateLayoutChanges is set to true, the ViewGroup is provided with a default LayoutTransition object as shown in the code here.

So far, you may be wondering what is this secret I was talking about.

Chances are you had already used this “automagical” attribute in the past and it worked for you as expected so you didn’t even bother to check the documentation. But, same chances are you only used it to animate adding/removing a child or changing said child’s visibility. Have you ever tried to set this attribute and change some child’s size ? If you did, you may have noticed there was no animation ! Yet, there is an easy fix for that !

But before we go further, let me illustrate the situation. Let’s assume we have the following layout :

Code below

Pay attention that I have set the attribute android:animateLayoutChanges to true. Now, let’s assume we want to change the text to something longer when clicking on it :

And let’s watch the result of this code :

Even though we had added the android:animateLayoutChanges attribute to our LinearLayout the change didn’t trigger an animation. To fix that, let’s add a little bit of code :

As you can see, the method enableTransitionType was added in the API 16 (Jelly Bean). Nevertheless, at the time I’m writing those lines, supporting API 16 as minimum is supporting 98.40% of active devices so that’s not really a problem.

And voilà :

This is what the documentation says about the LayoutTransition.CHANGING flag:

A flag indicating the animation that runs on those items that are changing due to a layout change not caused by items being added to or removed from the container. This transition type is not enabled by default; it can be enabled via enableTransitionType(int).

There is no indication why this has been disabled by default. My guess is it is disabled by default because it was added after the others and was disabled to maintain legacy compatibility by default.
The other flags were added at the same time LayoutTransition was (API 11) and LayoutTransition.CHANGING was added at the same time enableTransitionType was (API 16).

Finally, the documentation doesn’t mention any performance hit regarding this particular flag or regarding the use of LayoutTransition so we can assume it is safe to use. Nevertheless, if that wasn’t the case for you, please note you can provide your own implementation of LayoutTransition or re-implement something similar by yourself.

--

--