skd
skd

Reputation: 1

How to use Chronometer in kotlin jetpack compose android?

I'm new to Android development and learning about the jetpack compose and kotlin. In my learning process I'm trying to build a timer app. I don't know how use an existing chronometer library with jetpack compose and kotlin.

I found android.widget.Chronometer but is it only for xml or can it be used with jetpack compose? If yes then how?

Upvotes: 0

Views: 619

Answers (1)

Jan Itor
Jan Itor

Reputation: 4266

Yes, you can use Views in Compose - Using Views in Compose. For example:

@Composable
fun MyTimer() {
    Column {
        var chronometer: Chronometer? = null
        TextButton(onClick = { chronometer?.start() }) {
            Text("Start")
        }
        TextButton(onClick = { chronometer?.stop() }) {
            Text("Stop")
        }
        AndroidView(
            factory = { context ->
                Chronometer(context).also { chronometer = it }
            },
        )
    }
}

Upvotes: 0

Related Questions