Since Android 3.0 (API level 11), a lot of effort has gone into improving the visual appeal of the operating system. In addition to adding eye candy to the builtin software, Google also introduced new features in the Android development frameworks that make it easier for third-party developers to improve the look and feel of their own applications. One such improvement is the ability to add animations to existing ViewGroup elements (i.e. every layout object) without having to write the code by hand.

When using the android:animateLayoutChanges attribute with a ViewGroup, a set of default animations are registered on the ViewGroup‘s child views during layout changes. By default, visibility and size modifications are taken into account. The first produces a fading effect and the second produces a sliding animation.

These two default effects can be used together with a LinearLayout to make a nice transition between two states of the same widget:

In the video above, my widget has two different presentation modes: one for user selection and the other for drag-and-drop interaction. The latter mode is triggered by a user action (starting the drag).

Here is the relevant part of my layout XML for the widget skeleton:

The outer LinearLayout is the one that contains the android:animateLayoutChanges attribute. Both widget modes are also LinearLayout objects.

Since the first mode is the one shown by default, its layout height is set to wrap_content whereas the second mode layout is artificially hidden by setting its height to 1 pixel.

To go from mode 1 to mode 2, I use the following code in the drag started event handler:

And to go the other way, we simply restore the XML state we had beforehand with a similar code: