A MotionLayout is a ConstraintLayout which allows you to animate layouts between various states. MotionLayout links to and requires a MotionScene file.
MotionScene
A MotionScene file can contain all that is needed for specifying the animation:
the ConstraintSets used
the transition between those ConstraintSets
keyframes, touch handling, etc.
A ConstraintSetlets you create and save constraints, and apply them to an existing ConstraintLayout
Motion scene files usually contain two constraint sets: one for the beginning of the animation and one for the end.
Make sure that the ConstraintLayout import version is at least ConstraintLayout 2.0.0-alpha1.
Switching to a MotionLayout
The MotionLayout is a subclass of ConstraintLayout. So to transform a ConstraintLayout to a MotionLayout we need to only replace the class name from :
To create our MotionScene we first need to create an XML directory in our resources file. Then we create a new XML file, called motion_scene_main.xml
Our MotionScene should contain a transition attribute where we will specify the starting and ending constraints, how long it should last and we are also going to add a gesture handler.
As we mentioned above, the MotionLayout links to the MotionScene. But right now our MotionLayout is still not aware that we created that file. So we need to go back to the activity_main_start.xml file, add a layoutDescription attribute to the widget, and set its value to the name of the motion scene file.
If we now try to enter the design mode of that layout we will see that it cannot render. The reason for this is that all the widgets found in the starting constraint should also be included to the ending one and vice versa. So we need to copy paste the views we haven't added so far to both of our layouts.