{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

AndroidPopupKt

public final class AndroidPopupKt


Summary

Public methods

static final void
@Composable
Popup(
    @NonNull PopupPositionProvider popupPositionProvider,
    Function0<Unit> onDismissRequest,
    @NonNull PopupProperties properties,
    @Composable @NonNull Function0<Unit> content
)

Opens a popup with the given content.

static final void
@Composable
Popup(
    @NonNull Alignment alignment,
    @NonNull IntOffset offset,
    Function0<Unit> onDismissRequest,
    @NonNull PopupProperties properties,
    @Composable @NonNull Function0<Unit> content
)

Opens a popup with the given content.

static final boolean

Returns whether the given view is an underlying decor view of a popup.

Public methods

Popup

@Composable
public static final void Popup(
    @NonNull PopupPositionProvider popupPositionProvider,
    Function0<Unit> onDismissRequest,
    @NonNull PopupProperties properties,
    @Composable @NonNull Function0<Unit> content
)

Opens a popup with the given content.

The popup is positioned using a custom popupPositionProvider.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup

Box {
    val popupWidth = 200.dp
    val popupHeight = 50.dp
    val cornerSize = 16.dp

    Popup(alignment = Alignment.Center) {
        // Draw a rectangle shape with rounded corners inside the popup
        Box(
            Modifier
                .size(popupWidth, popupHeight)
                .background(Color.White, RoundedCornerShape(cornerSize))
        )
    }
}
Parameters
@NonNull PopupPositionProvider popupPositionProvider

Provides the screen position of the popup.

Function0<Unit> onDismissRequest

Executes when the user clicks outside of the popup.

@NonNull PopupProperties properties

PopupProperties for further customization of this popup's behavior.

@Composable @NonNull Function0<Unit> content

The content to be displayed inside the popup.

Popup

@Composable
public static final void Popup(
    @NonNull Alignment alignment,
    @NonNull IntOffset offset,
    Function0<Unit> onDismissRequest,
    @NonNull PopupProperties properties,
    @Composable @NonNull Function0<Unit> content
)

Opens a popup with the given content.

A popup is a floating container that appears on top of the current activity. It is especially useful for non-modal UI surfaces that remain hidden until they are needed, for example floating menus like Cut/Copy/Paste.

The popup is positioned relative to its parent, using the alignment and offset. The popup is visible as long as it is part of the composition hierarchy.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup

Box {
    val popupWidth = 200.dp
    val popupHeight = 50.dp
    val cornerSize = 16.dp

    Popup(alignment = Alignment.Center) {
        // Draw a rectangle shape with rounded corners inside the popup
        Box(
            Modifier
                .size(popupWidth, popupHeight)
                .background(Color.White, RoundedCornerShape(cornerSize))
        )
    }
}
Parameters
@NonNull Alignment alignment

The alignment relative to the parent.

@NonNull IntOffset offset

An offset from the original aligned position of the popup. Offset respects the Ltr/Rtl context, thus in Ltr it will be added to the original aligned position and in Rtl it will be subtracted from it.

Function0<Unit> onDismissRequest

Executes when the user clicks outside of the popup.

@NonNull PopupProperties properties

PopupProperties for further customization of this popup's behavior.

@Composable @NonNull Function0<Unit> content

The content to be displayed inside the popup.

isPopupLayout

@TestOnly
public static final boolean isPopupLayout(@NonNull View view, String testTag)

Returns whether the given view is an underlying decor view of a popup. If the given testTag is supplied it also verifies that the popup has such tag assigned.

Parameters
@NonNull View view

View to verify.

String testTag

If provided, tests that the given tag in defined on the popup.