Orchestra icon indicating copy to clipboard operation
Orchestra copied to clipboard

How should I use ComposeView to setLayout in Balloon?

Open wosika opened this issue 3 years ago • 2 comments

  • Library Version com.github.skydoves:orchestra-balloon:1.1.1

  • Affected Device(s) Android 10

Describe the Bug:

I try to write like this

BalloonAnchor(
            reference = row,
            balloon = Balloon.Builder(LocalContext.current).apply {
                setLayout(ComposeView(LocalContext.current).apply {
                    setContent {
                        // In Compose world
                        MaterialTheme {
                            Text("Hello Compose!")
                        }
                    }
                })
            }.build(),
            onAnchorClick = { balloon, anchor ->
                balloon.showAsDropDown(anchor)
            }
        )

it crashed

  java.lang.IllegalStateException: Cannot locate windowRecomposer; View androidx.compose.ui.platform.ComposeView{8b981b1 V.E...... ......I. 0,0-0,0} is not attached to a window
        at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:225)
        at androidx.compose.ui.platform.AbstractComposeView.resolveParentCompositionContext(ComposeView.android.kt:244)
        at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:251)
        at androidx.compose.ui.platform.AbstractComposeView.onMeasure(ComposeView.android.kt:288)
        at android.view.View.measure(View.java:25477)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6981)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25477)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6981)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25477)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6981)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25477)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6981)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25477)
        at com.skydoves.balloon.Balloon$showAsDropDown$$inlined$show$1.run(Balloon.kt:747)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:217)
        at android.app.ActivityThread.main(ActivityThread.java:8002)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:502)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)

wosika avatar Nov 10 '21 07:11 wosika

@wosika Have you found a way to use a custom layout with Compose since ?

hoboris avatar Dec 22 '22 02:12 hoboris

You can try the solution I've used elsewhere @hoboris

    val composeView = ComposeView(context).apply {
			setContent { }

   val frameLayout = FrameLayout(context).apply {
                         //core
			id = android.R.id.content
			ViewTreeLifecycleOwner.set(this, lifecycleOwner)
			setViewTreeSavedStateRegistryOwner(saveOwner)
		

			layoutParams = FrameLayout.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT
			)
			addView(composeView)
		}
		contentView = frameLayout
}



val lifecycleOwner = LocalLifecycleOwner.current
val stateRegistryOwner = LocalSavedStateRegistryOwner.current

wosika avatar Dec 22 '22 03:12 wosika