android-jetpack-composeandroid-jetpackandroid-jetpack-compose-text

How to show a composable just for e few seconds?


I have a Text composable within a Box:

Box(modifier = Modifier)
) {
    Text(text = "BlaBla"    )
}

How can show the Box/Text for a few seconds, only?


Solution

  • You can use LaunchedEffect and delay with a boolean flag and set it false after time specified

    @Composable
    private fun TimedLayout() {
        var show by remember { mutableStateOf(true) }
    
        LaunchedEffect(key1 = Unit){
            delay(5000)
            show = false
        }
        Column(modifier=Modifier.fillMaxSize()) {
            Text("Box showing: $show")
            if(show){
                Box{
                    Text(text = "BlaBla"    )
                }
            }
        }
    }