When you start to learn Android Development, it can be interesting to make some little and fun applications. It’s good to motivate yourself and it is also a great way to discover some specific part of the Android SDK. Today, I propose you to create a Flip Coin Application on Android to help you to discover how to use the Animation API.
Note that you can discover this tutorial directly in video on Youtube :
To create a Flip Coin Application, the first thing is to have two images for the head and the tail of the coin. We will use a Minnesota Quarter Coin here :
The second step is to define an ImageView and a Button in the layout of the Main Activity :
Now, we can write the Java code. The most interesting part of our application is implemented in the flipCoin() method. We create two animations :
A Fade Out animation to let the coin disappear when the user will click on the button to flip the coin
A Fade In animation to let the coin appear after we flip the coin randomly
The Fade Out and Fade In animations are created via the AlphaAnimation class on Android. We are going to animate the alpha property from 1 to 0 for the Fade Out effect and then from 0 to 1 for the Fade In effect.
We add an AnimationListener on the Fade Out animation to be sure to flip the coin and to start the Fade In animation just when the Fade Out effect will be ended.
The code for the Main Activity will have the following form :