Home > Net >  Jetpack Compose - How to fill Box with View
Jetpack Compose - How to fill Box with View

Time:03-05

I'm migrating from standard Android to Jetpack Compose but I'm having a problem. I don't know how to "convert" the following implementation:

Standard Android:

  1. I call a function and receive a View.
  2. I use the FrameLayout addView() method call to fill my frameLayout with the received view.

How can I do it in compose?

In compose use a frameLayout means to use a Box but I don't know how I can fill it with my received view.

Could anyone suggest me the correct way?

CodePudding user response:

You can do something like:

    Box(
        modifier = Modifier.fillMaxSize()
    )

CodePudding user response:

Just use a List<View> and loop over it. Adding elements into the list will trigger recomposition.

@Composable
fun MyOldFrameLayout(modifier: Modifier = Modifier, dynamicViews: List<View>) {
  Box(modifier.fillMaxSize()) {
    dynamicViews.forEach { view -> AndroidView(factory= { view }) }
  }
}
  • Related