Constraints not working Jetpack compose for AndroidView

544 Views Asked by At

I'm designing a layout where we have some layout at top and an adview at bottom of the screen. For this I'm using below code.

 ConstraintLayout(
                modifier = Modifier
                    .fillMaxSize()
                    .background(colorResource(id = R.color.purple_200))
            ) {

                val (scannerView, adView) = createRefs()

                Text(text = "Hello composable", modifier = Modifier
                    .constrainAs(scannerView) {
                        start.linkTo(parent.start)
                        end.linkTo(parent.end)
                        bottom.linkTo(parent.top)
                    })

                AndroidView(modifier = Modifier
                    .constrainAs(adView) {
                        start.linkTo(parent.start)
                        end.linkTo(parent.end)
                        bottom.linkTo(parent.bottom)
                    },factory = { context ->
                    AdView(context).apply {
                            setAdSize(AdSize.BANNER)
                            adUnitId = context.getString(R.string.banner_id)
                            loadAd(AdRequest.Builder().build())
                        }
                })

            }

But issue i'm facing is the AdView is not getting laid out at bottom instead it is ignoring constraints and showing on top of the screen. The same thing is working for other composable like Text or Image etc but not for only AndroidView. Any help is appreciated. Thanks in advance.

1

There are 1 best solutions below

1
Hussnain Hashmi On

After lot of searching I have wrapped my AndroidView in Column. For reference check the below code.

Column(modifier = Modifier
                    .constrainAs(adView) {
                        start.linkTo(parent.start)
                        end.linkTo(parent.end)
                        bottom.linkTo(parent.bottom)
                        width = Dimension.fillToConstraints
                    }
                    .background(colorResource(id = R.color.black)),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    AndroidView(factory = { context ->
                        AdView(context).apply {
                            setAdSize(AdSize.BANNER)
                            adUnitId = context.getString(R.string.banner_id)
                            loadAd(AdRequest.Builder().build())
                        }
                    })
                }