Code for Countdown timer in Android app

Activity Forums Android mobile app Code for Countdown timer in Android app

  • This topic is empty.
  • Creator
    Topic
  • #127
    adcocitclass
    Keymaster

    Using android.os.CountDownTimer, following count down timer code is designed.It will run according to milliseconds specified in below logic

    import java.util.concurrent.TimeUnit;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.widget.TextView;

    public class MainActivity extends Activity {

    TextView text1;

    private static final String FORMAT = "%02d:%02d:%02d";

    int seconds , minutes;

    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text1=(TextView)findViewById(R.id.textView1);

    new CountDownTimer(16069000, 1000) { // adjust the milli seconds here

    public void onTick(long millisUntilFinished) {

    text1.setText(""+String.format(FORMAT,
    TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
    TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
    }

    public void onFinish() {
    text1.setText("done!");
    }
    }.start();

    }

    }

  • You must be logged in to reply to this topic.