androidkotlinandroid-motionlayout

How to check if MotionScene is at transition "start" or "end"?


I have a MotionLayout with a MotionScene, and I want to do an "if-check" (in Kotlin) that checks if the scene is currently at transition "start" or "end".

Does anyone know how to do this?

Here is my transition "start" and "end" in the MotionScene:

<Transition
     motion:constraintSetEnd="@+id/end"
     motion:constraintSetStart="@+id/start"
     motion:duration="1000"> 

Solution

  • You can play with TransitionListener callbacks as your need

    motionLayout.setTransitionListener(new MotionLayout.TransitionListener() {
            @Override
            public void onTransitionStarted(MotionLayout motionLayout, int i, int i1) {
                Log.i(TAG, "onTransitionStarted: ");
            }
    
            @Override
            public void onTransitionChange(MotionLayout motionLayout, int i, int i1, float v) {
                if (isViewVisible) {
                    hideViews();
                }
            }
            @Override
            public void onTransitionCompleted(MotionLayout motionLayout, int i) {
                if (i != R.layout.start) {
                    showViews();
                }
            }
    
    
            @Override
            public void onTransitionTrigger(MotionLayout motionLayout, int i, boolean b, float v) {
    
    
            }
        });
    

    you can also use motionLayout.getProgress()
    inside onTransitionChange which called whenever a drawer's position changes.

    like so

     public void onTransitionChange(MotionLayout motionLayout, int i, boolean b, float v) {
                if (motionLayout.getProgress() == 0.0) {
                  // this is start 
                } else {
                    // this is end 
                }
            }