MainActivity.kt
package com.cfsuman.jetpackcompose
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.filled.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
GetScaffold()
}
}
@Composable
fun GetScaffold(){
val scaffoldState: ScaffoldState = rememberScaffoldState(
snackbarHostState = SnackbarHostState()
)
val visibleState:MutableState<Boolean> =
remember { mutableStateOf(false) }
val snackBarMessage:MutableState<String> =
remember { mutableStateOf("This is a snackbar") }
Scaffold(
scaffoldState = scaffoldState,
topBar = {
TopAppBar(
title = { Text(
text = "Compose - Scaffold Snackbar host"
)},
backgroundColor = Color(0xFFC0E8D5),
)
},
content = {MainContent(visibleState,snackBarMessage)},
backgroundColor = Color(0xFFEDEAE0),
snackbarHost = {
if (visibleState.value){
Snackbar(
action = {
TextButton(
onClick = { visibleState.value = false }
) {
Text(text = "Hide")
}
},
content = {
Text(text = snackBarMessage.value)
},
backgroundColor = Color(0xFFFF9966)
)
}
}
)
}
@Composable
fun MainContent(
visibleState:MutableState<Boolean>,
snackbarMessage:MutableState<String>
){
val scope = rememberCoroutineScope()
var counter by remember { mutableStateOf(0)}
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
){
Button(onClick = {
scope.launch{
counter++
visibleState.value = true
snackbarMessage.value =
"Hello, i am a snackbar ($counter)"
}
}) {
Text(text = "Show Snackbar")
}
}
}
}
- jetpack compose - Snackbar action
- jetpack compose - Snackbar dismiss listener
- jetpack compose - Dismiss Snackbar programmatically
- jetpack compose - Snackbar host state
- jetpack compose - AndroidView click event
- jetpack compose - AndroidView modifier
- jetpack compose - How to use bottom navigation
- jetpack compose - Kotlinx serialization decode josn element
- jetpack compose - How to use kotlin flow
- jetpack compose - Random number flow
- jetpack compose - flowOf flow builder
- jetpack compose - Convert list to flow
- jetpack compose - Count down flow
- jetpack compose flow - Room add remove update
- jetpack compose flow - Room implement search