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

4.6k views Asked by At

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"> 
4

There are 4 answers

0
Mohamed AbdelraZek On BEST ANSWER

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 
            }
        }
1
Tob237 On

I found a very convenient MotionLayout method that can be used, and solves my problem.

One can simply use MotionLayout's built-in method "progress" like this:

if (my_motionlayout.progress == 0.0f) {
           // The transition is at "start"
        }
1
Moudou On

instead of using

motionLayout.progress == 0.0f

you can use

motionLayout.currentState == R.id.XXXX

and change XXXX with the state you want, for the first state use start as mentioned below

motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
        override fun onTransitionCompleted(motionLayout: MotionLayout?, currentId: Int) {
            if (motionLayout != null) {
                if (motionLayout.currentState == R.id.start) {

                    // PUT YOUR CODE HERE

                }
            }
        }

        override fun onTransitionTrigger(motionLayout: MotionLayout?, triggerId: Int, positive: Boolean, progress: Float) {}
        override fun onTransitionStarted(motionLayout: MotionLayout?, startId: Int, endId: Int) {}
        override fun onTransitionChange(motionLayout: MotionLayout?, startId: Int, endId: Int, progress: Float) {}
    })
0
Isuru Gunasekara On
int startStateId = R.id.start; // replace 'start' with your actual start state ID
int endStateId = R.id.end; // replace 'end' with your actual end state ID

// Check if the current state is the start state
if (motionLayout.getCurrentState() == startStateId) {
    // The MotionLayout is at the start state
}

// Check if the current state is the end state
if (motionLayout.getCurrentState() == endStateId) {
    // The MotionLayout is at the end state
}