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 = {

})


If you want to show 2 buttons horizontally with equal split you can use Row and modifier property with weight with something like this :

@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

Popular posts from this blog

Using TabLayout and ViewPager with CollapsingToolbarLayout

Using android BadgeDrawable to show the Badge android

styling android TabLayout material design