androidkotlinandroid-motionlayout

How to Stop Motion Layout animation when data is fit to screen


I used Motion Layout for Collapse Layout. Motion layout working perfect as I need but I want to set motion layout animation based on data fit too the screen.

Like if data fit in screen then no need to animation. If data out of the screen then show animation.


Solution

  • Finally I got solution:

    motionLogin?.enableTransition(R.id.transitionLogin, false)
            constraintLayout.viewTreeObserver.addOnGlobalLayoutListener(object :
                ViewTreeObserver.OnGlobalLayoutListener {
    
                override fun onGlobalLayout() {
                    // If the scrollView can scroll, disable the accept menu item button
                    if (constraintLayout.canScrollVertically(1) || constraintLayout.canScrollVertically(
                            -1
                        )
                    ) {
                        motionLogin?.enableTransition(R.id.transitionLogin, true)
                    }
    
                    // Remove itself after onGlobalLayout is first called or else it would be called about a million times per second
                    constraintLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
                }
            })