RecyclerView android:layoutAnimation not working

Niharika Arora
3 min readMar 16, 2019

--

RecyclerView Layout Animation

Experience is what you get when you didn’t get what you wanted :-)

Sharing one more problem I had while implementing content animation for a recyclerView.

android:layoutAnimation stopped working. This can happen -

  • on the first run
  • Specific devices

I am sure If you are into Android development you must have implemented RecyclerView. So I am not going into the definition and all here.

I recently got a screen where I was required to add an initial content animation for a RecyclerView.

There are many ways of doing this, e.g.:

  • Implementing a custom ItemAnimator
  • Add animation handling to onBindViewHolder() in the Adapter

I used the easy one that is LayoutAnimation. (Also, I am focusing more on RecyclerView here but we can apply layoutAnimation to any subclass of ViewGroup)

Now There were one or two more animations on the same screen happening before this recyclerView gets visible.

My Sample RecyclerView XML -

So in OnAnimationEnd of other View, I turned on the visibility for this recyclerView.

Everything seems fine here right. I tried it on some devices and this was working exactly according to the requirements.

So there was one device Motorola One power-Android Pie(One of those specific devices). When I launched my app in this device, this attribute stopped working. Strange but I had to fix this up. I read about layoutAnimation and here is the correct way of using it-

Wait before that you should know why was this occurring?

The issue is visibility. When a view is initially set as gone in the xml file, Android will not render the layout until the visibility is changed to visible or invisible. If you attempt to animate on a view that has not been rendered yet, the animation will occur on the view without a layout. After the animation has completed, it will render it and suddenly the view will appear without animation. It works subsequent times because the view has a layout even when set to gone.

The key is to set the visibility to invisible instead of gone in the xml file so that it is rendered yet hidden. When the animation occurs the view appear and will move as expected.

So I changed the visibility from gone to invisible -

So you may have this question now that why wasn’t this the case with every device?

Animation behavior is dependent on -

  • View Hierarchy
  • View Drawing
  • When the view is getting drawn

Please feel free to share your opinion in comments.

If you liked this blog, hit the 👏 . Stay tuned for the next one!

📝 Read this story later in Journal.

🗞 Wake up every Sunday morning to the week’s most noteworthy Tech stories, opinions, and news waiting in your inbox: Get the noteworthy newsletter >

--

--

Niharika Arora

Googler since 2021 | Ex- Google Developer Expert, Android | Visit : https://thedroidlady.com/ to know more.