Android – Building a Fidget spinner (Part 1)

Recently i have seen many people building fidget spinners apps  so i decided to take some time and build my own fidget spinner app and post it online.

Note: This application is built completely in Kotlin and was done using Android studio 3.0 (Alpha). this is not a Kotlin tutorial, for those of you who do not know how to setup Kotlin for android development, please see HERE.

The goal of this tutorial is very straight forward, create an application with an ImageView that rotates and vibrates the phone on press.

Lets start with the XML

The xml is pretty straight forward, theres a ConstraintLayout with a single image view inside of it.

Now lets take a look at the only activity in the application, the MainActivity.

First we want to add an onTouchListener to the ivFidget. This is what will notify us when the spinner has been touched. In order to do this we do the following on the onCreate of the activity.

There are two events we want to listen to. When the user presses the image and when they let go. In order to do this we do:

First things first, lets vibrate the device when the spinner is touched, and lets stop vibrating when the spinner is released.

The last and final piece of this entire application is to spin the spinner using a RotateAnimation.

Thats it. Run the app and when the image view is clicked the image will spin and vibrate the device. For the full source code of this please visit HERE.

This is all for this post, hope you guys enjoy it. In the next post we will take a look on how to spin and move the spinner at the same time.

Until next time.