Search code examples
androidandroid-motionlayoutwindowinsets

Motion layout does not apply window insets


ViewCompat.setOnApplyWindowInsetsListener(fab) { view, insets ->
        val lp = fab.layoutParams as ConstraintLayout.LayoutParams
        lp.bottomMargin += insets.systemWindowInsetBottom
        fab.layoutParams = lp
        insets
    }

Motion layout does not pass any window insets to children in spite of consuming the insets explicit in the OnApplyWindowInsetsListener listener.

The insets are applied correctly when applyMotionScene attribute of motion layout is set to false.


Solution

  • with(ml) { //ml -> your motionLayout id
                        updateState(R.id.start, ConstraintSet().apply {
                            clone(ml)
                            constrainHeight(viewWhichHeightNeedsToChange.id, height.dp + insets.systemWindowInsetTop)
                            applyTo(ml)
                        })
                        setState(R.id.end, ml.width, ml.height)
                        updateState(R.id.end, ConstraintSet().apply {
                            clone(ml)
                            constrainHeight(viewWhichHeightNeedsToChange.id, height.dp + insets.systemWindowInsetTop)
                            applyTo(ml)
                        })
                        setState(R.id.start, ml.width, ml.height)
                    }
    

    Basically you need to update padding/size of your views in both motion layout sets. Would be great if there is a way to do so without switching between states though. This code is executed in setOnApplyWindowInsetsListener { } works with

    androidx.constraintlayout:constraintlayout:2.0.0-beta3