Transition X — Better transitions with Kotlin

What Kotlin has to do with transitions and Material Motion? Let’s find out.

Material Choreography provides certain guidelines on how these animations should play; namely ordering, easing, duration and type.

The transition framework is already capable of that. But the boilerplate required for choreographing detailed transition were immense. Let’s take an example. Android lets us create TransitionSet instance by using XML as shown below.

Android studio has auto completion for common properties.

It is no argument that XML takes lot of words to convey structure and that grows linearly when you want to choreograph complex transitions. Later in the code you would use TransitionInflater to get an instance then call TransitionManager.beginDelayedTransition. This has high chance of getting repeated all over the place.

Using XML also breaks type safety when working with custom transition instances.

I set to solve this problem using Kotlin by taking advantage of lamdas with receivers feature and reducing much of the boilerplate. To achieve the same transition, Transition X code would look like below:

Transition X provides a prepareTransition extension method on ViewGroup classes which can be used to construct complex transition instances. After prepareTransition you typically apply the layout changes for kick starting the transition. Scheduling call to TransitionManager is handled automatically, you only need to concentrate on declaring different transitions and their characteristics and the library handles the rest.

Transition X library features

Declarative — Easily declare variety of transitions and specify their properties. No math required. Transition X has variety of methods for inbuilt transitions from support library like ChangeBounds , ChangeTransform , ChangeClipBounds etc.

Easily declare variety of transitions and specify their properties. No math required. Transition X has variety of methods for inbuilt transitions from support library like , , etc. Rich tooling support — Transition X inherits all of Kotlin’s powerful IDE tooling integrations and provides you with auto complete for all available transition and related properties.

IDE autocomplete for all available transitions and properties

Extensible — Have a custom transition? Transition X can work with them too, thanks to Kotlin’s reified inline types.

Natural syntax — The syntax is simple to understand and reduces cognitive load needed to follow the animation choreography. Lifecycle methods like onEnd , onStart provide greater clarity and control over how rest of the code reacts to transition.

Transition Lifecycle listeners

The remainder of the article will explore some common transitions and demonstrate how they are accomplished with transiton x.