https://kotlinlang.org logo
#compose-android
Title
# compose-android
m

Mark

12/11/2023, 4:11 PM
Anyone managed to show an
AnnotatedString
in a
Toast
message?
z

Zach Klippenstein (he/him) [MOD]

12/11/2023, 4:23 PM
Like a legacy, view-based toast? First suggestion would be to use Snackbar in Material Compose instead
1
You could theoretically do this with a custom Toast view as well, but toasts with custom views have been deprecated in favor of snackbars in API 30.
j

Joel Denke

12/11/2023, 8:35 PM
Can always use ComposeAbstractView inside Toast ;) Would create my custom snackbar host in compose. Can use in Scaffold or custom impl. And override Snackbar style going Toast look. Then can do whatever you want easy.
m

Mark

12/12/2023, 4:00 AM
Yes, I tried the
ComposeView
approach but got a
ViewTreeLifecycleOwner not found
exception, and figured it wasn’t worth going down that route. So yeah I agree better to migrate away from
Toast
altogether at this point.
@Zach Klippenstein (he/him) [MOD] I’m migrating a large app from View-based to Compose. There are about 30 places where Toasts are invoked. Almost always these are places in the code where the UI is not immediately available programmatically, but perhaps a
Context
is, and so it’s been a case of just using the
Context
or passing one down, rather than a more verbose system of event handling. These
Toast
messages are usually something very simple, like “no items” when realising a list is empty and so aborting some workflow. A more concrete use case where a
Toast
is preferable is while a
Dialog
is being shown, in which case the snackbar is shown behind and so, not only dimmed and clickable, but also sometimes obscured.
z

Zach Klippenstein (he/him) [MOD]

12/12/2023, 4:30 PM
For the custom view issue, it’s not hard to wire up, but there might be edge cases with lifecycle/toast interactions that I might give you trouble. I’ve never tried. More info in this thread
thank you color 1
m

Mark

12/13/2023, 4:23 AM
What’s the preferred Compose way to show such messages while displaying a Compose
Dialog
?
5 Views