https://kotlinlang.org logo
a

Apoorv Patel

10/15/2020, 4:40 PM
Hi, I am trying to use 
ConstraintLayout
  but i am not able to get the constraints to work properly.
Copy code
ConstraintLayout(modifier = Modifier.padding(top = 16.dp, bottom = 16.dp)) {
        val (profileImage, progressText1) = createRefs()

        Image(
                modifier = Modifier
                        .constrainAs(profileImage){
                            start.linkTo(parent.start, 16.dp)
                        }.preferredWidth(72.dp)
                        .preferredHeight(72.dp)
                        .background(Color.Green),
                asset = imageResource(R.drawable.unknown_profile_empty)
        )

        val half = createGuidelineFromTop(0.5f)

        Text(text = "Sample text", modifier = Modifier.background(Color.Red).constrainAs(progressText1) {
            bottom.linkTo(half, 0.dp)
            start.linkTo(profileImage.end, 0.dp)
            end.linkTo(parent.end, 16.dp)
        })
    }
I am expecting that the text will align itself to the image end but it is not aligned to that
h

Hitanshu Dhawan

10/15/2020, 4:50 PM
Try removing the end constraint
Copy code
end.linkTo(parent.end, 16.dp)
Here, it is having start and end constraints, so it is centred from image end and parent end.
a

apoorv9990

10/15/2020, 5:24 PM
oh I was assuming it would stretch itself to fill the whole space. What do I need to do to have the behavior?
If I remove the end constraint it will behave like wrap_content
h

Hitanshu Dhawan

10/15/2020, 5:45 PM
Try this
Copy code
Text(text = "Sample text", modifier = Modifier.background(Color.Red).constrainAs(progressText1) {
            bottom.linkTo(half, 0.dp)
            start.linkTo(profileImage.end, 0.dp)
            end.linkTo(parent.end, 16.dp)
            width = Dimension.fillToConstraints
        })
Copy code
width = Dimension.fillToConstraints
a

Apoorv Patel

10/16/2020, 2:59 AM
Thanks! I didn't know about the width
2 Views