Showing Alert Dialog with compose example android
With new compose library you can show alert dialog using the android compose below is the example :
@Composable
fun showDialog(message: String, onDismiss: () -> Unit) {
AlertDialog(
text = { Text(message) },
onDismissRequest = onDismiss,
buttons = {
Column {
Divider(
androidx.compose.ui.Modifier.padding(horizontal = 12.dp),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.2f)
)
Column {
TextButton(
onClick = onDismiss,
shape = RectangleShape,
contentPadding = PaddingValues(16.dp),
modifier = androidx.compose.ui.Modifier.fillMaxWidth(),
) {
Text("Yes")
}
TextButton(
onClick = onDismiss,
shape = RectangleShape,
contentPadding = PaddingValues(16.dp),
modifier = androidx.compose.ui.Modifier.fillMaxWidth()
) {
Text("No")
}
}
}
}
)
}
above example we are using column because we want to show 2 buttons vertically.
to execute above method you can call like this.
showDialog("Sample Text\n\nSample Text\n\nSample Text", onDismiss = {
})
@Composable
fun showDialog(message: String, onDismiss: () -> Unit) {
AlertDialog(
text = { Text(message) },
onDismissRequest = onDismiss,
buttons = {
Column {
Divider(
androidx.compose.ui.Modifier.padding(horizontal = 12.dp),
color = MaterialTheme.colors.onSurface.copy(alpha = 0.2f)
)
Row {
TextButton(
onClick = onDismiss,
shape = RectangleShape,
contentPadding = PaddingValues(16.dp),
modifier = androidx.compose.ui.Modifier.weight(1.0f, true),
) {
Text("Yes")
}
TextButton(
onClick = onDismiss,
shape = RectangleShape,
contentPadding = PaddingValues(16.dp),
modifier = androidx.compose.ui.Modifier.weight(1.0f, true),
) {
Text("No")
}
}
}
}
)
}
Comments
Post a Comment