androidkotlinchronometer

chronometer.start method doesn't work propierty


Okay I was trying to make a simple stopwatch as a practice and I can add all components the problem is that I can't start the cronometer that I have in my main layout.

this is the code of my app:

class MainActivity : ComponentActivity() {
    private lateinit var btnStart: Button
    private lateinit var btnStop: Button
    private lateinit var btnReset: Button
    private lateinit var chronometer: Chronometer

    private var pausedAt: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main)
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        btnStart = findViewById<Button>(R.id.btn_start)
        btnStop = findViewById<Button>(R.id.btn_stop)
        btnReset = findViewById<Button>(R.id.btn_restart)
        chronometer = findViewById<Chronometer>(R.id.chrono)

        btnStart.setOnClickListener(View.OnClickListener { startChrono() })
        btnStop.setOnClickListener(View.OnClickListener { stopChrono() })
        btnStart.setOnClickListener(View.OnClickListener { resetChrono() })
    }

    private fun startChrono() {
        chronometer.base = SystemClock.elapsedRealtime()-pausedAt
        chronometer.start()
    }

    private fun stopChrono() {
        pausedAt = SystemClock.elapsedRealtime()-chronometer.base
        chronometer.stop()
    }

    private fun resetChrono() {
        pausedAt = 0
        chronometer.base = SystemClock.elapsedRealtime()
    }
}

I use the method chronometer.start() but it doesn't show changes so the app always shows 00:00


Solution

  • You have assigned the OnClickListener for the btnStart twice, and you missed assigning the correct listener for the btnReset.

    Here's the corrected code:

    class MainActivity : ComponentActivity() {
        private lateinit var btnStart: Button
        private lateinit var btnStop: Button
        private lateinit var btnReset: Button
        private lateinit var chronometer: Chronometer
    
        private var pausedAt: Long = 0
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.main)
            window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    
            btnStart = findViewById<Button>(R.id.btn_start)
            btnStop = findViewById<Button>(R.id.btn_stop)
            btnReset = findViewById<Button>(R.id.btn_restart)
            chronometer = findViewById<Chronometer>(R.id.chrono)
    
            btnStart.setOnClickListener(View.OnClickListener { startChrono() })
            btnStop.setOnClickListener(View.OnClickListener { stopChrono() })
            btnReset.setOnClickListener(View.OnClickListener { resetChrono() })
        }
    
        private fun startChrono() {
            chronometer.base = SystemClock.elapsedRealtime() - pausedAt
            chronometer.start()
        }
    
        private fun stopChrono() {
            pausedAt = SystemClock.elapsedRealtime() - chronometer.base
            chronometer.stop()
        }
    
        private fun resetChrono() {
            pausedAt = 0
            chronometer.base = SystemClock.elapsedRealtime()
        }
    }
    

    Now, the btnReset has the correct OnClickListener assigned. This should resolve the issue, and your chronometer should work as expected.