Search code examples
androidandroid-constraintlayoutandroid-jetpack-compose

constrainedWidth/constrainedHeight in Jetpack Compose with ConstraintLayout


How to set constrained width/height in Compose?

Text("Test", Modifier.constrainAs(text) {
    linkTo(
        start = parent.start,
        top = button.bottom,
        end = parent.end,
        bottom = parent.bottom,
        topMargin = 16.dp,
        horizontalBias = 0f
    )
    width = Dimension.wrapContent // this is default, can be removed
    // now I need to set constrainedWidth somehow like in XML app:layout_constrainedWidth 
})

width = Dimension.preferredWrapContent mb this one is equal to

 android:layout_width="wrap_content"
 app:layout_constrainedWidth="true"

?


Solution

  • using Dimension.preferredWrapContent worked for me:

    modifier = Modifier.constrainAs(content) {
        width = Dimension.preferredWrapContent
        linkTo(
            start = parent.start,
            top = button.bottom,
            end = parent.end,
            bottom = parent.bottom,
            topMargin = 16.dp,
            horizontalBias = 0f
        )
    }
    

    for more info - https://developer.android.com/reference/kotlin/androidx/constraintlayout/compose/Dimension.Companion#preferredWrapContent()