1

I tried showing an alert dialog over other apps. The permissions are granted.

<uses-permission android:name="ACTION_MANAGE_OVERLAY_PERMISSION"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

The Composable way is not working at all, codes follow:

AlertDialog(onDismissRequest = { dialogEnabled.value = false },
    title = { Text(text = "Test")},
    text = {
           Text(text = message)
    },
    confirmButton = {
        TextButton(onClick = { dialogEnabled.value = false }) {
            Text(text = "Confirm")
        }
    },
    dismissButton = {
        TextButton(onClick = { dialogEnabled.value = false }) {
            Text(text = "Cancel")
        }
    }
)

I got to set the type of the AlertDialog's window to WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY. However, I view the code of AlertDialog under AndroidDialog.android.kt, there no way to get the reference of dialog instance, not even the DialogProperty.

Eventually, I got use the traditional way to achieve it

private fun showDialog(message: String){
    val builder: AlertDialog.Builder = AlertDialog.Builder(this) //set icon
        .setIcon(android.R.drawable.ic_dialog_alert) //set title
        .setTitle("Game Analysis") //set message
        .setMessage(message) //set positive button
        .setPositiveButton(
            "Confrim"
        ) { dialogInterface, i -> //set what would happen when positive button is clicked
            dialogInterface.dismiss()
        } //set negative button
        .setNegativeButton(
            "Cancel"
        ) { dialogInterface, i -> //set what should happen when negative button is clicked
            dialogInterface.dismiss()
        }
    val alertDialog: AlertDialog = builder.create()
    alertDialog.window!!.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
    alertDialog.show()
}
JimJay Lee
  • 58
  • 7

1 Answers1

0

Custom AlertDialog with Jetpack Compose Android. Jetpack Compose is a great new declarative UI tool for Android that allows UI creation using Kotlin as a replacement for the cumbersome XML layouts.

enter image description here

//Create custom dialog
@Composable
fun DialogExample(
    onDismiss: () -> Unit,
    onNegativeClick: () -> Unit,
    onPositiveClick: () -> Unit
) {
    Dialog(onDismissRequest = onDismiss) {
        val color = Color(0xff4DB6AC)
        Card(elevation = 8.dp, shape = RoundedCornerShape(12.dp)) {
            Column {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(160.dp)
                        .background(color)
                ) {
                    Icon(
                        tint = Color.White,
                        painter = painterResource(id = R.drawable.ic_baseline_location_on_24),
                        contentDescription = null,
                        modifier = Modifier
                            .graphicsLayer(scaleX = 1.2f, scaleY = 1.2f)
                            .align(
                                Alignment.Center
                            )
                    )
                }

                Column(modifier = Modifier.padding(16.dp)) {
                    CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
                        Text("To send a nearby place or your location, allow access to your location.")
                    }
                    Row(
                        horizontalArrangement = Arrangement.End,
                        modifier = Modifier.fillMaxWidth()
                    ) {

                        Text(
                            text = "Not Now", color = color,
                            modifier = Modifier
                                .clickable(
                                    interactionSource = MutableInteractionSource(),
                                    indication = rememberRipple(color = Color.DarkGray),
                                    onClick = onNegativeClick
                                )
                                .padding(8.dp)
                        )

                        Spacer(modifier = Modifier.width(4.dp))
                        Text(
                            text = "Allow", color = color,
                            modifier = Modifier
                                .clickable(
                                    interactionSource = MutableInteractionSource(),
                                    indication = rememberRipple(color = Color.DarkGray),
                                    onClick = onPositiveClick
                                )
                                .padding(8.dp)
                        )
                    }
                }
            }
        }
    }

you can use this code for showing alert dialog