Thursday, February 21, 2019

Make count down clock Android

We wan to make a count down clock in game, give user 1 minute to try. When time finish, we show inform Game Over.
Declare a global variable CountDownTimer w;
Import library.
Copy to below findViewByid.
w=new CountDownTimer(60000, 1000) {           
  public void onTick(long mil) {
   tv.setText("Seconds remaining: " + mil/1000);
  }      
public void onFinish() {
tv.setText("Seconds remaining: 0");
Intent in = new Intent(MainActivity.this, newclass.class);
startActivity(in);
}
}.start();  
If time still remaining but user press Back button, we need to cancel CountDown.
Copy to above last bracket.
Override
public void onBackPressed()
{        
w.cancel();
finish();

}   

No comments:

Post a Comment