Question: 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

Answer: 
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;
        }
    }
});

```
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