Jetpack compose: How to flow current time

The code demonstrates how to continuously display the current time using Jetpack Compose. It achieves this by leveraging a flow and a state composable.

  1. Flow to emit time:

    • A flow named timerFlow is created that emits a string value every 1 second.
    • Inside the flow, the getTime function is called to get the current time in hh:mm:ss format.
    • The retrieved time is then emitted using the emit function.
  2. Composable to display time:

    • The MainContent composable is responsible for displaying the current time.
    • It defines a function named getTime that retrieves the current time as a string.
    • A state variable named currentTime is created using collectAsState which initially holds the value returned by the getTime function.
    • The currentTime is then displayed within a Text composable with a font size of 40sp.

Summary

The code effectively utilizes a flow to periodically update the current time and a state composable to display it on the screen. This approach ensures that the UI stays up-to-date with the latest time.


MainActivity.kt

package com.cfsuman.composestate

import android.os.Bundle
import android.text.format.DateFormat
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.sp
import com.cfsuman.composestate.ui.theme.ComposeStateTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import java.util.*


class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeStateTheme {
                Scaffold(
                    topBar = { TopAppBar(
                        title = { Text(text = "Compose - Flow Current Time")}
                    ) },
                    content = { MainContent() },
                    backgroundColor = Color(0xFFEDEAE0)
                )
            }
        }
    }
}


@Composable
fun MainContent() {
    fun getTime():String{
        return DateFormat.format("hh:mm:ss",
            Date(System.currentTimeMillis())).toString()
    }

    val timerFlow = flow<String> {
        delay(1000)
        emit(getTime())
    }

    val currentTime by timerFlow
        .collectAsState(initial = getTime())

    Box(
        Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = currentTime,
            style = TextStyle(fontSize = 40.sp)
        )
    }
}
More android jetpack compose tutorials