Animating Android Binding Transitions

With the new Android binding framework, it’s easy to automatically update your view based on model changes. You just need to make your model observable and the framework will take care of it. For example, you can show and hide a loading indicator just by doing

What is less obvious, is how you would go about animating that change. For example, what if you want to fade the progress bar in and out?

One solution is to use a custom  @BindingAdapter to apply the animation

However, this has a problem: the animation will run every time the value changes, even when it’s bound the first time. For example, you’ll see it fade in or out on rotation. You need a way to determine if this is the first binding or not.

You can take advantage of the fact that view tags are not persisted, if the tag is not there, this is the first binding and instead of running the animation, set the tag.

And there you go! The loading indicator will nicely fade in and out as you model changes.

1 Comment

  1. Nice! This technique could be extended to many different animated transitions. Actually from architectural point of view this is the proper way to isolate the view animation specifics.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and be notified of new posts by email.