0

I created Linear Layout with two custom views.

class myView1(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
...
 override fun onDraw(canvas: Canvas?){..}
}

class myView2(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
...
 override fun onDraw(canvas: Canvas?){..}
}

I have button. I would like to have situation like this:

  • If button clicked: start loop which will be calling onDraw every second (refreshing View every second)
  • If button clicked once again: break the loop.

I was trying by .invalidate(), but it didn't work.

How can I call create that loop?

Klaudia
  • 3
  • 1

1 Answers1

0

I can't write a comment, so I will answer, you didn’t write exactly how it doesn’t work invalidate() method, invalidate() problem, in any case, I added a loop where you need to call invalidate()

  var timer = Timer()
            var isTimerWork = false
    
            btn.setOnClickListener {
                if (isTimerWork) {
                    isTimerWork = false
    
                    timer.cancel()
                    timer = Timer()
                } else {
                    isTimerWork = true
    
                    timer.scheduleAtFixedRate(object : TimerTask() {
                        override fun run() {
    //                       invalidate view
                        }
                    }, 0, 1000)
                }
            }
Yura
  • 363
  • 3
  • 10