0

I want to make a countdown timer like the one below, but using the kotlin language I did not find a suitable article on the Internet for this subject.

timeCount=70000;

        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        timeCount-=1000;
                        txtTimer.setText(getTimeStyle(timeCount));
                        if (timeCount==0){
                            timer.cancel();
                            txtTimer.setText("00:00");
                            txtResend.setVisibility(View.VISIBLE);
                            txtResend.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    finish();
                                }
                            });
                        }

                    }
                });
            }
        },0,1000);

Asghar Hosseini
  • 239
  • 1
  • 5
  • 14

1 Answers1

3

You can use Kotlin objects:

val timer = object: CountDownTimer(70000, 1000) {
    override fun onTick(millisUntilFinished: Long) {...}

    override fun onFinish() {...}
}
timer.start()
S T
  • 1,068
  • 2
  • 8
  • 16
  • 1
    nice how you just copied it from here: https://stackoverflow.com/a/54095975/1788806 without mentioning the source. plus, you changed 20000 to 70000 so that it wouldn't be obvious right away. wow! – Willi Mentzel Nov 10 '20 at 09:29
  • 1
    Is the answer he gave wrong or right? I agree he should have reference but does that really warrant the comment? hmmm – Asuquo12 Aug 06 '21 at 23:40