One Off the Slack: How Do We Use ComposeView in an AutoCompleteTextView?
maxgdn asked:
Have any attempts at putting a ComposeView inside an ArrayAdapter been made?
(my initial reaction: 😮)
maxgdn continued:
After varied attempts of
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
[and] callingdisposeComposition()
I’m stuck with
java.lang.IllegalStateException: Cannot locate windowRecomposer; View androidx.compose.ui.platform.ComposeView{...} is not attached to a window
It is used in
AutoCompleteTextView
for the dropdown items.
While the question was phrased in terms of ArrayAdapter
, the real problem
lies with AutoCompleteTextView
.
While a fair bit of work was done to support Compose UI integration with
legacy views, maxgdn is breaking new ground. Or, as Google’s Adam Powell put it:
I have to admit you’re the first person to ask 😀
Adam went on with the solution, such as it is:
it’ll be kind of ugly but your entry point for what it’s trying to do is here
the composition needs a parentCompositionContext set on the view to be able to measure, and AutoCompleteTextView tries to measure unattached views to determine the size of the popup window to create
if a parentCompositionContext isn’t present, the ComposeView will search the view tree for one. If it’s attached to a window then a window-scoped recomposer will be lazily created at that point
you can install one yourself in the window where the AutoCompleteTextView is attached by doing what this internal property does
it should make things easier that we recently opened the create function for this as experimental, so you can at least call that to create one instead of having to copy all of that 🙂
that might only be in the recent alphas, I forget if it was experimental in 1.1 or only
internal
Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!