Composables
Docs Login
Tally

Alert Dialog

Jetpack Compose Component

Download
package com.composables.tally.components

import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.composables.tally.theme.ProvideContentColor
import com.composables.tally.theme.Space
import com.composables.tally.theme.Theme
import com.composeunstyled.DialogPanel
import com.composeunstyled.Scrim
import com.composeunstyled.UnstyledDialog

@Composable
fun AlertDialog(
    visible: Boolean,
    title: String,
    message: String,
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier,
    actions: @Composable ColumnScope.() -> Unit,
) {
    UnstyledDialog(
        visible = visible,
        onDismissRequest = onDismissRequest,
        overlay = {
            Scrim(scrimColor = Color.Black.copy(alpha = 0.6f), enter = fadeIn(), exit = fadeOut())
        },
    ) {
        Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            DialogPanel(
                modifier =
                    modifier
                        .padding(Space * 6)
                        .widthIn(max = 400.dp)
                        .clip(Theme.shapes.large)
                        .background(Theme.colors.surface),
                paneTitle = title,
                enter = scaleIn(initialScale = 0.94f) + fadeIn(),
                exit = scaleOut(targetScale = 0.94f) + fadeOut(),
            ) {
                ProvideContentColor(Theme.colors.onSurface) {
                    Column(Modifier.padding(Space * 6)) {
                        Text(title, style = Theme.typography.title)
                        Text(
                            message,
                            color = Theme.colors.onBackgroundMuted,
                            modifier = Modifier.padding(top = Space * 2),
                        )
                        Column(
                            Modifier.fillMaxWidth().padding(top = Space * 6),
                            verticalArrangement = Arrangement.spacedBy(Space * 2),
                            content = actions,
                        )
                    }
                }
            }
        }
    }
}

More components from Tally

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.