Is there a way to show a Compose popup or dialog f...
# compose
e
Is there a way to show a Compose popup or dialog from an Android custom view? We have lots of Android custom views that we don't have the time to migrate fully to compose, but we are adding new developments in compose gradually. Adding
ComposeView
in the XML works for most cases, but now we have to show a pop window and an alert on user interaction. We have tried creating the
PopupWindow
and
AlertDialog
programmatically and setting the content view as a
ComposeView
with the desired content but we get errors like:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from android.widget.PopupWindow$PopupDecorView
So we are thinking that maybe there is a way to launch the alert and popup directly from compose, but don't know how to launch it from inside a custom view without replacing the already working content.
m
wont a regular callback work?
e
What do you mean with a regular callback in this context?
m
you said you wanted to show a compose dialog from a customView. I am assuming you are using ComposeView as root to host your regular legacy view code, including your custom view. Otherwise, why would you want to display a compose dialog?. If the answer is yes, i’m suggesting to use a callback. If the answer is no, i don’t thing there is anything build-in compose that would allow you to do that
oh i’m sorry, for some reason i didnt read the last part
e
Ok, let me explain what do I have more precisely and see if were I am going wrong: I have a custom view that inherits from ConstraintLayout with all kinds android views. The new requirement is to show a tooltip when the user clicks some of this android views. We already have the tooltip built in compose and wanted to use it here. I have built a function that sets the
contentView
of the PopupWindow to a
ComposeView
but this fails due to the lifecycle error of my original message. I was wondering if maybe there is a way to add a dummy ComposeView to the ConstraintLayout and launch the tooltip from there
We have the same exact issue with AlertDialogs
m
did you set
Copy code
setViewCompositionStrategy
in your compose dialog view?
e
yes, tried with all available plus creating a custom one that follows the PopupWindow, all with the same error
m
are you using the latest version o ConstraintLayout ?
e
how do I check it? I updated the app compat and lifecycle versions, but don't know if I need to update something else
m
appcompat version is greater than 1.3.0 ?
e
yes, 1.4.1
m
hm weird. Maybe some1 can help you - i don’t think there is other way of doing that different from how you are doing. But that error is indeed strange
e
In the case of PopupWindow I tried calling
ViewTreeLifecycleOwner.set()
and setting it manually, but the view that crashes es private and I am no sure how to do it
m
can you use androidx.compose.material.AlertDialog and add your custom tooltip inside ?
e
How would I use the compose AlertDialog from the custom view?
a
You may be able to workaround the viewtree errors doing what appcompat itself does for activites and fragments. We have a helper like this you can apply to an alertdialog or dialog before you show it, and then the composeview won't throw --
Copy code
// use the right lifecycle owner, activity shown here

fun Dialog.enableComposeViewSupport(activity: AppCompatActivity) {
    val decorView = getWindow()?.getDecorView()
    if (decorView != null) {
        ViewTreeLifecycleOwner.set(decorView, activity)
        ViewTreeViewModelStoreOwner.set(decorView, activity)
        ViewTreeSavedStateRegistryOwner.set(decorView, activity)
    }
}
another option is to create your alertdialog via a dialogfragment, in that case you won't need this workaround.
e
we ended up using a dummy composeView in the xml layout that launches the dialog directly from compose. Your solution seems more elegant, we will test it if similar requirements arrive in the future. Thank you everyone.
432 Views