gtboy
gtboy

Reputation: 128

Timer in Android

I'm a beginner in android. Now i'm working on a simple app. I want to create a timer in app. I want it to countdown from 10 to 0 (it will be visible to the user) and when it is 0, it should do smth. It should start counting down when onTouch event is called. I tried this way, but it doesn't work. Can anyone help please?

here's my code:

final MyCounter timer = new MyCounter(10000,1000);

public class MyCounter extends CountDownTimer{

        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            System.out.println("Timer Completed.");
            time.setText("Timer Completed.");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            time.setText((millisUntilFinished/1000)+"");
            System.out.println("Timer  : " + (millisUntilFinished/1000));
        }
    }
        public boolean onTouchEvent(MotionEvent event) {
          if(event.getAction()==MotionEvent.ACTION_DOWN){
             timer.start();
          }
          return false;

Upvotes: 1

Views: 547

Answers (1)

jstock
jstock

Reputation: 116

The timer code looks correct. Let me check some assumptions: 1) "time" is a TextView 2) You actually hook the onTouchEvent up to something.

Set a breakpoint in onTouchEvent and verify it's getting called. Also, take a look at the Log class and LogCat to verify the methods are getting called.

Upvotes: 1

Related Questions