Chronometer in Android using Kotlin:

CHRONOMETER IN ANDROID

Chronometer in Android is useful to implement a simple Timer. It is a subclass of TextView however holds a countdown or count up timer.

Steps to implement chronometer in Android are given below

First step is to add some strings in strings xml

Write strings given below


Next step to design layout

Add 4 buttons

Set constraints and I’d for buttons

First button text start

Second stop

Third button apply format

Fourth button for clear format

Add chronometer in activity main xml-

 In main activity 

Write code given below


view.onclicklistener

Override fun on create(saved instance saved:bundle?){

super.on create(saved instance state)

set content view(R.layout.activity_main

Btn_clear_format.setonclicklistener

btn_start.setonclicklistener

btn_stop.setonclicklistener

btn_apply_format.setonclicklistener

timer.setonclicklistener{

Toast.maketext(context this,main activity,text “tick happened”,Toast.LENGTH_LONG).show()

}

override fun on click(p0:view?}{

when(p0?.Id)

{

R.id.btn_stop->timer.stop()

R.id.btn_start->timer.start()

R.id.btn_format->timer.format()=”timer is running:

else-> timer.format=null

Finish

Leave a Reply

Your email address will not be published. Required fields are marked *