How to show Alert Dialog in Android Jetpack Compose

By Mohammed Rashid •  November 18th, 2022 • 

Alert dialogs help developers to show important messages or updates to mobile app users. In this Jetpack Compose tutorial, let’s learn how to show a simple alert dialog.

AlertDialog is the function we have to use for this purpose. It helps to show an alert dialog with buttons.

@Composable
fun AlertExample() {
    val dialogVisibility = remember { mutableStateOf(true) }
    if (dialogVisibility.value) {
        AlertDialog(
            onDismissRequest = {
                dialogVisibility.value = false
            },
            title = {
                Text(text = " Alert Title")
            },
            text = {
                Text("This is a Jetpack Compose tutorial on AlertDialog.")
            },
            buttons = {
                Row(
                    modifier = Modifier.padding(10.dp),
                    horizontalArrangement = Arrangement.Center
                ) {
                    Button(
                        modifier = Modifier.fillMaxWidth(),
                        onClick = { dialogVisibility.value = false }
                    ) {
                        Text("Okay")
                    }
                }
            }
        )
    }
}

In the above code snippet, the dialogVisibilty variable controls the visibility of the AlertDialog. The use of title, text, buttons, and onDismissRequest parameters is self-explanatory.

Following is the complete code for this AlertDialog example.

package com.example.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    color = MaterialTheme.colors.background,
                    modifier = Modifier.fillMaxWidth().fillMaxHeight()
                ) {
                    AlertExample()
                }
            }
        }
    }
}

@Composable
fun AlertExample() {
    val dialogVisibility = remember { mutableStateOf(true) }
    if (dialogVisibility.value) {
        AlertDialog(
            onDismissRequest = {
                dialogVisibility.value = false
            },
            title = {
                Text(text = " Alert Title")
            },
            text = {
                Text("This is a Jetpack Compose tutorial on AlertDialog.")
            },
            buttons = {
                Row(
                    modifier = Modifier.padding(10.dp),
                    horizontalArrangement = Arrangement.Center
                ) {
                    Button(
                        modifier = Modifier.fillMaxWidth(),
                        onClick = { dialogVisibility.value = false }
                    ) {
                        Text("Okay")
                    }
                }
            }
        )
    }
}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MyApplicationTheme {
        AlertExample()
    }
}

Following is the output.

That’s how we show a AlertDialog in Jetpack Compose.

Mohammed Rashid

Keep Reading