MainActivity.kt
package com.cfsuman.composestate
import android.os.Bundle
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 kotlin.random.Random
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeStateTheme {
Scaffold(
topBar = { TopAppBar(
title = { Text(text = "Compose - Random Number Flow")}
) },
content = { MainContent() },
backgroundColor = Color(0xFFEDEAE0)
)
}
}
}
}
@Composable
fun MainContent() {
val randomNumberFlow = flow<Int> {
delay(1000)
emit(Random.nextInt())
}
val randomNumber by randomNumberFlow
.collectAsState(initial = Random.nextInt())
Box(
Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(
text = "$randomNumber",
style = TextStyle(fontSize = 40.sp)
)
}
}
- jetpack compose - Kotlinx serialization build json array
- jetpack compose - Kotlinx serialization build json object
- jetpack compose - Kotlinx serialization decode josn element
- jetpack compose - How to use kotlin flow
- jetpack compose - How to use Button
- jetpack compose - Extended floating action button example
- jetpack compose - How to use Snackbar
- jetpack compose - How to use BottomAppBar
- jetpack compose - Image from vector
- jetpack compose - Image tint
- jetpack compose - String resource plurals
- jetpack compose - LazyColumn scroll to position
- jetpack compose - Keyframes animation
- jetpack compose - AnimateDpAsState
- jetpack compose - Draw image on canvas