Shindra
Shindra

Reputation: 95

Constraint layout Y position not matching the margin value

I'm facing some sort of trouble setting constraint on a view on runtime with a margin.

These are the y and margin coordinate when the view is displayed to the user enter image description here

I set the margin like that :

    connect(
            chipsId,
            ConstraintSet.TOP,
            viewId,
            ConstraintSet.TOP,
        )

        connect(
            chipsId,
            ConstraintSet.BOTTOM,
            viewId,
            ConstraintSet.BOTTOM,
            145
        )

This give me this output from the layout inspector

enter image description here

The margin is set to 145px. But when you do the calculation the y is reduce to only half the value of the margin. Y1 - Y2 = 472 - 399 = 73 =~ 145/2.

I do not understand why the y has only move up from half the desire margin. Did i miss something in the constraint layout ? (We are using the latest stable version : 2.1.4 )

Thank you

Upvotes: 0

Views: 75

Answers (1)

goemic
goemic

Reputation: 1339

It is really hard to follow the actual problem because there is not a lot of information - it is unclear how your layout (should) look like.

But the problem seems to be that your are setting the bottom margin in reference between bottom of chipsId and bottom of viewId.

Can you share at least the (xml) specification for the View with chipsId and the one for viewId?

Depending on the order you probably have to set the margin from TOP to BOTTOM, e.g. like this:

    connect(
        chipsId,
        ConstraintSet.BOTTOM,
        viewId,
        ConstraintSet.TOP,
        145
    ) 

This would result in a margin of 145 between the bottom of chipsId and the top of viewId

Upvotes: 0

Related Questions