I haven’t filed an issue yet, which I can do soon.
Sharing the code snippet,
@Composable
fun RandomColorAndroidView(modifier: Modifier = Modifier) {
AndroidView(
modifier = modifier,
factory = { context ->
FrameLayout(context).apply {
setBackgroundColor(getRandomColor())
}
},
update = { view ->
// update the view if needed
},
)
}
@Composable
fun RandomColorFrameLayoutsInConstraintLayout() {
ConstraintLayout(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.background)
) {
val (androidView1Ref, androidView2Ref, androidView3Ref) = createRefs()
RandomColorAndroidView(
modifier = Modifier
.size(50.dp)
.constrainAs(androidView1Ref) {
start.linkTo(parent.start)
top.linkTo(<http://parent.top|parent.top>)
bottom.linkTo(parent.bottom)
}
)
RandomColorAndroidView(
modifier = Modifier
.size(40.dp)
.constrainAs(androidView2Ref) {
start.linkTo(androidView1Ref.end)
top.linkTo(<http://parent.top|parent.top>)
bottom.linkTo(parent.bottom)
}
)
RandomColorAndroidView(
modifier = Modifier
.size(30.dp)
.constrainAs(androidView3Ref) {
start.linkTo(androidView2Ref.end)
top.linkTo(<http://parent.top|parent.top>)
bottom.linkTo(parent.bottom)
}
)
}
}
private fun getRandomColor(): Int {
val hue = Random.nextInt(360)
val saturation = Random.nextFloat()
val value = Random.nextFloat()
return ColorUtils.HSLToColor(floatArrayOf(hue.toFloat(), saturation, value))
}