Motion layout start animation at start

Viewed 1099

I've been watching motion animation layout videos and I've seen that the animation gets activated after a click or a swipe, how can I make the animation start when all the views have been created, like for example in a Hero Animation?

1 Answers

You could use methods transitionToStart / transitionToEnd on MotionLayout to perform animations from code. And therefore you can call this methods in onStart/onResume Fragment or Activity methods for example. https://developer.android.com/reference/androidx/constraintlayout/motion/widget/MotionLayout#transitionToEnd()

Example:

class MyFragment: Fragment() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // Start animation programmatically after all views created
        motion_layout.transitionToEnd()
    }
}
Related