Learn to create a Slot Machine for Android

In that tutorial, you are going to discover how to create a Slot Machine for Android with Android Studio. A Slot Machine, also known as one-armed bandit, is a casino gambling machine with three or more reels which spin when a button is pushed. Our Slot Machine will have three reels. Note that you can also enjoy this tutorial in video on YouTube :

Our Slot Machine will have three reels which will display the following fruits’ images :

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

To start, we write the code of the layout of our Slot Machine. The layout consists to three images for each reel, a button to start to spin the reels and a TextView which will be useful to display a message to the user like : You win the big prize !

Now, we are going to create the logic of our Slot Machine. The logic will be implemented in a Wheel object. To animate the reels, our Wheel class will extends the Thread from the Android SDK.

Thus, each wheel will be animated in a dedicated thread. We define a WheelListener interface to let the main activity to be notified when an image changes for the wheel.

The images which will be displayed by the wheel are defined in a static imgs array. The currentIndex property will let us to know the current image displayed by the wheel. The frameDuration property will be used to define the duration of a frame for the wheel. A frame is the time the wheel keep the same image. Finally, the startIn property will lets us to start the wheels in a delay defined in the constructor. Thus, each wheel could start at a different moment chosen randomly.

In the run method of our Wheel object, we make a pause a startIn milliseconds. The, we enter in a loop which will be active until the isStarted property be false. In the loop, we change the current image when a frame duration is passed. When we have displayed all the images, we return to the first image of the array.

When a new image is displayed, we notify the WheelListener object which has subscribed to the Wheel by calling the newImage method. That gives us the following code for the Wheel :

Last step for our Slot Machine Application is to write the Java code of the Main Activity. We declare three Wheel objects. One for each reel of our Slot Machine.

We create a static randomLong method to return a random long value between lower and upper values passed in parameters.

In the onCreate method we set an OnClickListener object on the button used to launch or stop the Slot Machine. We wheck the state of the Slot Machine stored in the isStarted boolean property. If the Slot Machine is started, we stop the three wheels by calling their stopWheel method. Then, we check the current image displayed to display the message to the user. If the three images are the same, the user wins the big prize !

If the Slot Machine is not started, we create the three wheels. For each wheel, we define a WheelListener interface implementation in each we are going to update the image displayed when the nextImg method is called. Like the wheels are executed in a dedicated thread, we need to define that the update of the User Interface will be made inside the UI Thread.

Finally, we start the wheels at a different moment by using the randomLong method. The code of the Main Activity will be like that :

You can enjoy your new Slot Machine Application for Android. Run your application and you should have the following screen on your Android device :

Image for post
Image for post

If you want to discover more tutorials on Android development, don’t hesitate to follow my blog : https://www.ssaurel.com/blog

Written by

Entrepreneur / Developer / Blogger / Author. In Bitcoin We Trust: https://www.inbitcoinwetrust.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store