Search code examples
androidcalendartextfieldcountdown

Setting a countdown to a specific time


I am trying to set a countdown to tuesday at 19:00. I have the switch case to find the day. But i am stuck in making the countdown. i will set the countdown to my textField "daag"

Please help, Thanks

    //To create switch case
    Calendar calendar = Calendar.getInstance();
    int day = calendar.get(Calendar.DAY_OF_WEEK);

    //Today
    final Calendar Today = Calendar.getInstance();

    //Target day's hour
    final Calendar tar = new GregorianCalendar();
    tar.add(Calendar.HOUR_OF_DAY, 19);


    final ScheduledExecutorService service = new ScheduledThreadPoolExecutor(1);


    switch (day) {
        case Calendar.MONDAY:
            daag.setText("Mandag");
            break;

        case Calendar.TUESDAY:

            service.schedule(new Runnable() {

            @Override
            public void run () {
                long diff = tar.getTimeInMillis() - Today.getTimeInMillis();
                long diffSec = diff / 1000;

                if (diff > 0) {
                  daag.setText("" + service.schedule(this, 1, TimeUnit.SECONDS);
                }
            }
        }, 1, TimeUnit.SECONDS);

            daag.setText("Tirsdag");
            break;

Solution

  • Try this

     new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
    
     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
      //here you can have your logic to set text to edittext
     }
    
     public void onFinish() {
         mTextField.setText("done!");
     }
    }
    .start();
    

    Refer this URL