I’m just starting with Jetpack Compose and have created a layout using XML’s ConstraintLayout. I’m trying to convert this layout to Jetpack Compose, but I’m unsure about the equivalent methods for layout_constraintHorizontal_bias
, layout_constraintHorizontal_chainStyle
, and layout_constrainedWidth
in Jetpack Compose.
Here is the complete XML code that I’m working with:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@+id/image"
android:text="lorem epsum loremsum loremlorem epsum loremsum loremlorem epsum loremsum loremlorem epsum loremsum loremlorem epsum loremsum lorem"
app:layout_constraintStart_toStartOf="parent"/>
<ImageView
android:id="@+id/image"
android:layout_width="14dp"
android:layout_height="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/textview"
app:layout_constraintBottom_toBottomOf="@id/textview"
app:layout_constraintStart_toEndOf="@+id/textview"
android:src="@drawable/ic_information_grey"/>
</androidx.constraintlayout.widget.ConstraintLayout>
What I tried is:
@Composable
fun CustomLayout(inputText: String) {
ConstraintLayout(modifier = Modifier.fillMaxWidth()) {
val (text, image) = createRefs()
val chain = createHorizontalChain(text,image, chainStyle = ChainStyle.Packed(0F))
constrain(chain){
start.linkTo(text.start)
end.linkTo(image.end)
}
Text(
text = inputText,
modifier = Modifier
.constrainAs(text) {
start.linkTo(parent.start)
end.linkTo(image.start)
top.linkTo(parent.top)
width = Dimension.preferredWrapContent
}.wrapContentSize()
)
Image(
painter = painterResource(id = R.drawable.ic_information_grey),
contentDescription = null,
modifier = Modifier
.size(14.dp)
.constrainAs(image) {
end.linkTo(parent.end)
top.linkTo(text.top)
start.linkTo(text.end)
width = Dimension.wrapContent
}.wrapContentSize()
)
}
}