'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 1:[1]

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)
            }
        })

Solution 2:[2]

For Java version:

 public static void enableDisableTransition(MotionLayout motionLayout, RecyclerView recyclerView){
    motionLayout.enableTransition(R.id.transition, false);
    recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (recyclerView.canScrollVertically(1) || recyclerView.canScrollVertically(-1 )
            ) {
                motionLayout.enableTransition(R.id.transition, true);
            }
            recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Salman khan