Managing MotionLayout visibility

Adrian Tache
Android Ideas
Published in
2 min readNov 23, 2020

If you’ve ever worked with MotionLayout, you know that it’s technically a drop-in replacement for ConstraintLayout which technically adds the ability to perform very complex keyframed animations quite easily. However, it has a tendency to be finicky and buggy at this point (for example, animations won’t run if your Fragment is still scrolling when you start them). One of the problems I’ve run into is that setting items to View.GONE doesn’t actually hide them. In other cases, I’ve had intermittent issues with CardViews sometimes being visible despite their content being invisible.

The problem is that, in a MotionLayout , the layout wants to control the visibility of its children in order to perform all the necessary animations etc. Of course, that means that when we want to hide something, we need to, instead of just setting the visibility of a View , modify the layout parameters of the MotionLayout itself to instruct it to modify that visibility. You can technically ask the MotionLayout to ignore the visibility of a certain view, but in my experience that doesn’t solve the problem.

So I found this method on StackOverflow, which I improved a bit with the help of Kotlin:

And just use this instead of the normal method whenever you’re in a MotionLayout (although you will probably need to rename it).

Hope this helps, I know I had a lot of issues with this bug!

Thanks for reading this article. You can connect with me on LinkedIn.

If you liked this article, please hit the clap icon 👏 to show your support.

--

--