2

I'm making a race timing app in Android Studio in kotlin that calculates a bunch of useful information off of split times. At the beginning of the race, bib 1 starts at 0, but bib 2 might start 15s later. I want the user to see each racer in a RecyclerView with a chronometer counting up (from for example -15) until that racer's start (0), and continue counting up as the race is going. What I've noticed is the Chronometer widget counts zero twice when it is starting from negative numbers and goes into positive numbers. Is there a way to have it only count zero once?

Below is code to set the chronometer inside of each cardview to the time until they start, and then starting them.

p0.athleteChrono.base = (SystemClock.elapsedRealtime() + racer.startTime *1000)
p0.athleteChrono.start()

Again, it sets itself correctly to, for example -00:10, starts counting up, but definitely displays -00:01, 00:00, 00:00, 00:01 before continuing up

Here is a sample main activity that just has a chronometer set to -3

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    btnStart.setOnClickListener {
        chronometer.base = SystemClock.elapsedRealtime() + 3000
        chronometer.start()
    }
}

}

and the sample layout file activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/chronometer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Chronometer"></TextView>

    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:padding="10dp"></Chronometer>
    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/chronometer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:background="@color/colorPrimary"
        android:text="Start"></Button>

</androidx.constraintlayout.widget.ConstraintLayout>
  • Ha, quite cool problem. And welcome to SO. Perhaps not using negative timers is an option, like you can use two timers, one from -15 to 0 and one from 1 -> future. So you observe the timer and when it's done, you fire the other? Some offset like that may work, but to be honest I haven't tried this. Or you can make the timer last one less second, but keep the number offset by 1? (I'm thinking out loud) – Martin Marconcini Oct 05 '19 at 11:37
  • thanks for the idea! I tried to set an onticklistener and reset the chrono base to try to skip over the -0... no luck, though I feel like that should have worked. I'll try the offset, or maybe I just do a countdown timer for each athlete until their start time and don't worry about displaying the running time – Michael Gibson Oct 07 '19 at 16:39
  • Let us know if you find something (or if you don't) and maybe -if all fails- you can produce a small working sample that I (or "we") can clone and experiment with :) It's an interesting problem. – Martin Marconcini Oct 07 '19 at 19:08
  • @MartinMarconcini – Michael Gibson Oct 10 '19 at 01:47
  • Apologies if that wasn't the format you wanted to see the 'sample' in. I'm pretty new to all of this, SO included – Michael Gibson Oct 10 '19 at 01:48

0 Answers0