Hi everyone. Is it possible to render a Compose co...
# compose
m
Hi everyone. Is it possible to render a Compose component on top of all other UI layers (including dialogs and popups) without requesting the
SYSTEM_ALERT_WINDOW
permission? I would like to create something like custom toasts.
j
On Android? Toasts let you specify custom views. It's deprecated but works fine. You can spin up a ComposeView in there. You gotta provide some of its dependencies like a lifecycle owner and state saver, but it's no different than shoving Compose UI in a custom Window in that regard.
m
But as you said, it's deprecated, so it might not be around for much longer. On top of that, my toasts have their own custom behavior—they stack underneath each other, have different display durations, and are positioned at the top of the screen. I assume this is not possible using toasts with custom views.
j
Sounds like you just want a overlay manager that sits at the top of your display hierarchy
You can render them using the exact same mechanism as popups and dialogs with a subcomposition rooted in a separate window
But otherwise, no, there's no magic +1 layer for windows because then something would use it and you would render behind it and come here asking for a +2 layer and so on
a
> Is it possible to render a Compose component on top of all other UI layers Yes. That's what
Dialog
does. It renders the content on its own platform window. This means that they will render on top of everything, BUT they don't allow clicks to anything behind them. > I would like to create something like custom toasts For this, I would not use a Dialog since you probably don't want to block the entire UI when your toasts are displayed. Snackbars in Material have their own component (snackbarhost iirc) that wraps the app/screen and places the rendered toast in front of everything else. I would do the same. In Unstyled, I am using a little utility called Portal that does exactly this (gives you a root level 'host' and allow you to render to it from anywhere in the UI tree). I use this for Unstyled Tooltips, because the original tooltips in compose are modal and I needed them to be non-modal.