Posts

Showing posts with the label Countdown timer

Boil an egg app - step by step (step 3 – code)

Image
  After reviewing part 2 of the tutorial, it’s time for coding. let’s get started. In the main activity class we’ll have the following things to do: 1. set some private variables to be accessed from different places in the class. 2. bind some variables to views from the xml reviewed in part 2 3. set listeners for the different buttons on the screen 4. handle timer changing and displaying time remained   let’s elaborate on this : 1. initialize some variables in the main class, place those prior to the onCreate() method:     private Button btnSmallEgg,btnMediumEgg,btnLargeEgg;     private Button btnCookAnEggTimer,btnResetTimer;     private TextView btnSoftEgg,btnHardEgg;     private TextView tvTimer,tvTimerLabel;     private TextView tvChooseEggSize,tvChooseEggType,tvIntro ;         long minutes;     long seconds;     long maxTimer=600...

A simple Countdown Timer in Android

Of course we can implement a simple timer using threads but there's a better way out of the box.      The Android SDK offers us a rather simple way to use timers in our apps, Let's see how it works: CountDownTimer timer = new CountDownTimer(maxmillis, 1000) { @Override public void onTick(long lastknowtimer) { int seconds = (int) (lastknowtimer / 1000) % 60 ; int minutes = (int) ((lastknowtimer / (1000*60)) % 60);   tvTimerLabel.setVisibility(View.VISIBLE); tvTimer.setText(String.valueOf(minuts*seconds));     } @Override public void onFinish() { tvTimer.setText("finished"); } }; The  abstract class -  CountdownTimer's constructor receives two parameters (maxmillis which is the amount of milliseconds to countdown from, and the second parameter is the interval in which the tick happens). It has two methods which must be implemented:   1. onTick()   - the ac...