Search code examples
androidandroid-motionlayout

MotionLayout jump to end ConstraintSet programmatically


There is an option to jump to end ConstraintSet in XML in MotionLayout.

<Transition
    app:autoTransition="jumpToEnd"
    motion:constraintSetEnd="@+id/constraintStart"
    motion:constraintSetStart="@+id/constraintEnd"
    motion:duration="300" />

Can this be done programmatically?


Solution

  • I figured it out. In order to jump to a ConstraintSet set the progress of MotionLayout to 1.

    motionLayout?.setProgress(1.0f)
    

    When you have multiple transitions, then set the state of MotionLayout to final ConstraintSet and then set the progress to 1.

    motionLayout?.setState(R.id.endConstraintSet,ConstraintSet.MATCH_CONSTRAINT,ConstraintSet.MATCH_CONSTRAINT)
    motionLayout?.setProgress(1.0f)