Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

?

like image 935
user924 Avatar asked Feb 14 '26 03:02

user924


2 Answers

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()

like image 164
Hadas Avatar answered Feb 15 '26 16:02

Hadas


You can use

width = Dimension.fillToConstraints
like image 43
Eddy Yoel Fresno Hernández Avatar answered Feb 15 '26 16:02

Eddy Yoel Fresno Hernández