'How to fill Box with View?
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:
- I call a function and receive a View.
- I use the FrameLayout
addView()method call to fill myFrameLayoutwith the received view.
How can I do it in composing?
In composing using a FrameLayout means to use a Box but I don't know how I can fill it with my received view.
Could anyone suggest to me the correct way?
here the code:
@Composable
fun MyCard(testActivity: Activity? = null) {
AdvertView(testActivity) { view ->
Box(modifier = Modifier.fillMaxSize()) {
AndroidView(factory = { view })
}
}
}
Solution 1:[1]
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 }) }
}
}
Based on what you provided on your screenshot
I suggest you refactor your screenshot into:
@Composable fun MyCard(onClick: () -> Unit) {
Box(modifier.fillMaxSize()) {
AndroidView(factory= { context -> AdvertView(context) { onClick() } })
}
}
Solution 2:[2]
You can do something like:
Box(
modifier = Modifier.fillMaxSize()
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Aldan |

