Arjun Kalburgi to the right of a sharp leaf

learning, finding, designing, developing impact

Creating a Custom Loading Spinner for Android 🤖

January 15, 2019

Recently at Krugo, I was implementing a custom loading animation. I don’t usually write about work things, but this one was different.

First, the Setup

First I implemented this BehaviorSubject in my Dependency Injection:

var progressBar: BehaviorSubject = BehaviorSubject.createDefault(false)
fun setProgressBar(value: Boolean) {

And subscribed to it from the MainActivity:

onNext = {
(it) {
this.mainProgress.visibility = View.VISIBLE } else {
this.mainProgress.visibility = View.GONE }
},onError = {})

These together allow me to toggle the ProgressBar view (that was set up in activity_main) simply by calling Injector.setProgressBar(**true**) and Injector.setProgressBar(**false**).

Second, the Animation

Here’s the fun part. I used Shape Shifter to make the animation, this is what I made:

Shape Shifter allows you to export the animation as an animated-vector , made just for Android.

I found that breaking that animated-vector into four separate files worked best. The first is the main file, make sure your ProgressBar uses this.


    android:animation="**@anim/your\_first\_animation\_file**" />  

    android:animation="**@anim/your\_second\_animation\_file**" />  


The vector file, taken from the <vector> section of the animated-vector :

<vector android:name="vector"
<group ... />
<group ... />

And the two animation files taken from the <target> sections and placed under res/anim/ . They’ll look like this:

<objectAnimator ... />
<objectAnimator ... />
<objectAnimator ... />
<objectAnimator ... />

Third, Making it Work

This was all great, but it didn’t exactly work. The animation wouldn’t repeat itself after it completed. Took a while but finally got it working by doing the following:

Change ProgressBar to ImageView

android:layout_gravity="center" />

Initialize the animation including registerAnimationCallback to force the looping of the animation.

val avd = AnimatedVectorDrawableCompat.create(applicationContext,
progress_bar).apply_ { setImageDrawable(avd)
obj:Animatable2Compat.AnimationCallback() {
**override fun
onAnimationEnd(drawable: Drawable?) {

To top it off, I added start() and stop() to my _subscribeBy_ method just to be nice:

onNext = {
(it) {
this.mainProgress.visibility = View.VISIBLE avd?.start() } else {
this.mainProgress.visibility = View.GONE avd?.stop() }
},onError = {})

This last step took me a while to figure out but I finally got it thanks to this article from the Android Developers blog:

If you were holding off adding awesome animation to your application because of lack of API support, then

Overall I loved the chance to work on some animation, I think it’s just going to become a bigger thing in 2019 and more of a priority for me to learn!

Krugo Travel

Quick shoutout to the company I’m working for, Krugo. I’m working on getting the Android app up and running hence why I was doing this little animation in Android!

Have you ever been frustrated trying to plan a trip with a group of people? Maybe a bachelor party, family vacation or trip with the girls? If so, you’re not alone.

And we’ve made it our mission to help.

Krugo helps groups of friends collaboratively plan trips by finding events, buying tickets and getting a place to stay. Can’t wait to take my own Krugo soon 😜

Check ’em out on Medium or get the app for iOS. Android coming soon!