How to Create a Simple Lucky Draw Spinning Wheel in Android?
- Android App Development Fundamentals for Beginners
- Guide to Install and Set up Android Studio
- How to Create/Start a New Project in Android Studio?
- Running your first Android app
- CountDownTimer in Android with Example
- Random Class in Java
We are going to build a lucky draw spinning wheel, that lets you spin a wheel when a button is clicked. This is a glimpse of the application we are going to build. The application contains a single Activity with a single TextView, two ImageView, and one Button to spin the wheel, A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add necessary assets
Now, go to app > res > drawable, and paste the following images to the drawable folder.
You can add any other similar images you want. But make sure to name the images as wheel.png and target.png respectively. Once you’ve pasted the images in the drawable folder, it will look something like this,
Step 3: Working with the activity_main.xml file
We have added the necessary resource files for the application we are building. Now, Let’s design the UI for our application. Add this XML file to app > res > layout. Below is the code for the activity_main.xml file.
Step 4: Working with the MainActivity.java file
Now it’s time to initialize everything in MainActivity. We are using Random class in java, to generate a random number to spin, and CountDownTimer class to rotate the image each millisecond by 2 degrees. Here is the complete code for MainActivity. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
That’s it. Now we can run the application. Make sure that your project contains all the following files.
Here is the preview of the final application.
Please Login to comment...