Anyone have any ideas why during build I get an er...
# compose
b
Anyone have any ideas why during build I get an error related to SurfaceView unresolved reference:
Copy code
@Composable
fun buildUi() {
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalGravity = Alignment.CenterHorizontally
    ) {
        SurfaceView(ContextAmbient.current)
    }
}
FWIW I have this problem with any View class: TextView, ImageView, et cetera
l
its a little bit hard to explain, but if you search this group for
emit
, a lot might show up. Compose’s compiler intercepts calls to android view constructors inside of composable functions. So basically, the constructor you are trying to call isn’t resolvable inside of a composable function (the IDE integration for this is still shotty which is one reason why we don’t talk about dealing with views this way very often)
to embed the SurfaceView into the hierarchy, what you actually want is just this
Copy code
SurfaceView()
any properties you want to set on the surface view can be set using a named parameter
Copy code
SurfaceView(id=123)
(as an example)
these will probably not show up as resolved in the IDE, but things will compile fine
if you want to actually construct the SurfaceView, btw, you can use the
escapeCompose
function:
Copy code
val view = escapeCompose { SurfaceView(context) }
b
That explains all the source code I see in the repo
I was so lost on how half of these Views had constructors I’d never seen before
Thanks for the help! 🙂
r
You can also use
AndroidView(layoutResourceId) { inflatedView -> /* … */ }
a
Speaking of integrating Android views in compose, is it possible yet to integrate WebView the same way? I noticed there was a WebComponent widget, but it didn't seem to have been published yet.
l
using WebView like i proposed above is possible, but getting it so that it works well and with some of the more imperative APIs exposed in a nicely declarative way is a bit of work. It can all be done in userland though. The WebComponent we have right now isn’t really very complete yet which is why it hasn’t been published yet