Start timer on button click

13,618

How does a timer work in Android?

You better read Timer documentation, CountDownTimer Documentation and Handler Documentation.

To the moment, when the button is clicked, all is cleared for me; but, how can I start the timer?

If I didn't misunderstand your question, when you say Timer, you refer to CounteDownTimer. So, you should have something like this:

(I've written a sample code. So, you should understand it first, and then, you should apply it to your code.)

Adding the Buttons

btn1 = (Button)findViewById(R.id.bt1);
btn2 = (Button)findViewById(R.id.bt2);

Adding the SetOnClickListener()

btn1.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {

    });
}

btn2.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {

    });
}

My btn1 starts the CountDownTimer, and the second one stops and clears it.

Now, I create an Inner Class with CountDownTimerTest name.

public class CountDownTimerTest extends CountDownTimer {
    public CountDownTimerTest(long startTime, long interval) {
        super(startTime, interval);
    }

    @Override
    public void onFinish() {
        text.setText("Time's up!");
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(startTime));
    }

    @Override
    public void onTick(long millisUntilFinished) {
        text.setText("Time remain:" + millisUntilFinished);
        timeElapsed = startTime - millisUntilFinished;
        timeElapsedView.setText("Time Elapsed: " + String.valueOf(timeElapsed));
    }
}

Then on my btn1, I put this code (start the CountDownTimer):

countDownTimer.start();

And on my btn2, I put this code (stop/cancel the CountDownTimer):

countDownTimer.cancel();

Now, I hope that you can understand how CountDownTimer works, if your question isn't about CountDownTimer, let me know, and I'll update my answer as soon as possible with your wishes.

EDIT - Only with one Button

To do it with the same Button, you can do this:

Create a Boolean variable as:

Boolean ButtonClicked = false;

And then, modify the code as follows:

btn1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        if (!ButtonClicked)) {
            ButtonClicked = true;
            countDownTimer.start(); 
        } else {
            ButtonClicked = false;
            countDownTimer.cancel();
        }                       
    });
}

EDIT 2 Get what button is clicked

You can create an int called NumberButtonClicked like this :

int NumberButtonClicked = 0;

Then on every Button you have you'll have to do this (Example) :

btn1.setOnClickListener(new OnClickListener() {
    @Override
        public void onClick(View arg0) {
            NumberButtonClicked = 1;
    });
}

Then you know that if you have clicked btn1 your variable will be 1.

Share:
13,618
Phil
Author by

Phil

Updated on June 05, 2022

Comments

  • Phil
    Phil almost 2 years

    I'm new in Android programming but i know Java. My question is, how does a timer work in Android? I've read that is better to use a handler. What I want to do is, you click a button and the timer starts. To the moment when the button is clicked all is clear for me but how to start the timer?