0

I am implementing a timer in android with one text view and one button for start/stop.

How do I set register different events on clicklistener of the same button, such that when it is clicked the first time it will start a timer and when clicked a second time it will stop the timer and report the time between events? I am implementing a timer in android with one text view and one button for start/stop.

How do I set register different events on clicklistener of the same button, such that when it is clicked the first time it will start a timer and when clicked a second time it will stop the timer and report the time between events?

Edit1

what i did is,

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_depth);
    findViewById(R.id.btn).setOnClickListener(this);

}
boolean showingFirst = true;
public void generate(View view){

    if(showingFirst){
        long startTime = System.currentTimeMillis();
        showingFirst = false;
    }else{
        long difference = System.currentTimeMillis() - startTime;
        showingFirst = true;
        TextView myText = (TextView)findViewById(R.id.tv);
        myText.setText(String.valueOf(difference));
    }

}

but since long starttime is started in if when the control enters else loop it shows cannot resolve symbol 'startTime'

please help and special thanks to eliamyro

  • you can use condition inside onClickListener, don't need different onClick. – Niranj Patel Jan 30 '18 at 13:10
  • could you please refer me to link describing that or give a little code snippet. Thank you –  Jan 30 '18 at 13:13
  • I believe that @NiranjPatel is referring to a simple `if` condition in the listener. I should be simple enough to use that if you have some prior Java knowledge. – Jakub Jankowski Jan 30 '18 at 13:39

4 Answers4

1

You can do it using a global boolean isStart and start or stop the timer depending on the value of the isStart.

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (isStart) {
            // Stop timer
            isStart = false;
        } else {
            // Start timer
            isStart = true;
        }
    }
});
Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
eliamyro
  • 308
  • 1
  • 4
  • 20
0

try this,

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(btn.getText().toString().equals("Start")){
            btn.setText("Stop");
            // start timer
        }else{
            btn.setText("Start");
            // stop timer
        }
    }
});
Bhavesh Desai
  • 753
  • 4
  • 20
0

on click (start/stop) button start the timer according to your code and return a value to button and when you click again that flag value can be used to create a if condition for stop as well as start

Madhu Nair
  • 428
  • 1
  • 7
  • 20
0

As you are starting and stopping a Timer with your button you can just check if the timer is running or not. I would suggest extending a TimerTask for that use case (I took that code from here):

public class YourTimerTask extends TimerTask {

    private boolean isRunning = false;

    @Overrides
    public void run() {
        this.isRunning = true;
        //rest of run logic here...
    }

    public boolean isRunning() {
        return this.isRunning;
    }
}

Then in your onClickListener you can just check if your timer is running or not:

startStopBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (yourTimer.isRunning()) {
            stopTimer();
        } else {
            startTimer();
        }
    }
});
Katharina
  • 1,612
  • 15
  • 27