An Egg Timer, also know as Kitchen Timer, is a device the primary function of which is to assist in timing during cooking. In that tutorial, you are going to create your an Egg Timer Application for Android.
Note that you can also discover this tutorial in video on YouTube :
Image for our Egg Timer
The first step is to find an egg image and putting it in the /res/drawable folder :
Like you can see, it is just a simple image but it will be perfect for our Egg Timer Android Application.
Defining strings of our Egg Timer
Now, we can define the strings we will use for our Egg Timer :
- Two for Start/Stop button
- One for the Start Time
- One for the default time
- A “Let’s eat !” message for alerting users the timer is finished
So, we have the following strings.xml file :
Defining the UI of our Egg Timer
The User Interface of our Egg Timer will be quite simple with an ImageView for displaying the Egg image, and EditText for letting user to enter time for the timer and finally a Button to start the Egg Timer.
We use a ConstraintLayout to place all these views on the screen :
Writing the logic code of our Egg Timer
The most important part of our Egg Timer is the logic code written in the MainActivity. For managing the timer, we will use a CountDownTimer object from the standard Android SDK. In the onCreate method, we find references for the EditText and the Button. We use a boolean indicating if the Egg Timer is started or no.
When the user clicks on the Button, we first check if the Egg Timer is started or no. If it’s started, we stop the Egg Timer by canceling the CountDownTimer instance which is running. We change the message on the Button and we enable the cursor on the EditText.
If the Egg Timer is stopped, we start it. For that, we disable the cursor on the EditText before getting the time value entered by the user in the EditText. If the value entered is not at the format min:sec, we set the default time.
After that, we instantiate a new CountDownTimer by passing in parameter the time for the timer. The second parameter indicates the onTick method of the CountDownTimer is called each 100 milliseconds. In the onTick method, we convert the millisUntilFinished value to extract remaining time in minute:second format. After that, we display the new value in the EditText.
When the CountDownTimer is finished, the onFinish method is called. We set default values and then, we display an alert message to the user for telling to the user it’s time to eat :). Finally, we post a delayed execution of a Runnable via a Handler to reset the timer to the default time after 1500 milliseconds.
It gives us the following code for the MainActivity :
Our Egg Timer in Action !
It’s time to put our Egg Timer in Action. We enter a time :
Then, we click on start, the timer runs and when the time is finished, a “Let’s Eat” message is appearing on the screen :
That’s all for that tutorial. If you have ideas for future tutorials. Don’t hesitate to tell me in comments !
If you want to get the latest news on how to make money online, don’t hesitate to visit Making Money Online Digest :
Making Money Online Digest
Get the latest info for making money online with Making Money Online Digest
Making Money Online Digest is a weekly email newsletter that helping you to get the latest info to make money online. Each day, new opportunities to earn money online appear.