Problem with Chronometer

Asked

Viewed 61 times

0

In the code, there is a variable running, indicating whether the timer is running or not:

boolean running = false;

And there’s a method StartStopTimer, that stops or starts the timer according to the variable:

   public void startStopTimer(View v){
    Chronometer chr = (Chronometer) findViewById(R.id.chronometer);
    Button ahj = (Button) findViewById(R.id.button4);
    if(running == false){
        chr.start();
        running = true;
        ahj.setText("Stop Timer");
    }
    else if(running == true){
        chr.stop();
        ahj.setText("Start Timer");
        running = false;
    }

}

Only when I run the app, the timer doesn’t start from scratch. If the application has been running for 15 seconds, for example, it starts from 15 seconds. How to make him start from 0?

Properties of the stopwatch:

 <Chronometer
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chronometer"
    android:textSize="100dp"
    android:layout_alignTop="@+id/valor1"
    android:layout_centerHorizontal="true" />

Thanks in advance :D

1 answer

0


Just restart the Counter Variable For example if the variable is called:

tempo

have to put

else if(running == true){
    tempo = 0;
    chr.stop();
    ahj.setText("Start Timer");
    running = false;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.