Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are createHorizontalChain and createHorizontalChainRtl swapped?

I was trying to create a constraint chain programmatically, using the code bellow.

set.createHorizontalChain(
                ConstraintSet.PARENT_ID, ConstraintSet.START,
                ConstraintSet.PARENT_ID, ConstraintSet.END,
                new int[] {view1.getId(), view2.getId()},
                null,
                ConstraintSet.CHAIN_PACKED
        );

I have getting the following error:

 Caused by: java.lang.IllegalArgumentException: Left to start undefined
                                                     at android.support.constraint.ConstraintSet.connect(ConstraintSet.java:922)
                                                     at android.support.constraint.ConstraintSet.createHorizontalChain(ConstraintSet.java:883)
                                                     at android.support.constraint.ConstraintSet.createHorizontalChain(ConstraintSet.java:850)

Then I realized that the implementation of this method is as follow:

public void createHorizontalChain(int leftId, int leftSide, int rightId, int rightSide, int[] chainIds, float[] weights, int style) {
        this.createHorizontalChain(leftId, leftSide, rightId, rightSide, chainIds, weights, style, 1, 2);
    }

    public void createHorizontalChainRtl(int startId, int startSide, int endId, int endSide, int[] chainIds, float[] weights, int style) {
        this.createHorizontalChain(startId, startSide, endId, endSide, chainIds, weights, style, 6, 7);
    }

Should I use createHorizontalChainRtl which seems to work, since 6 and 7 are the values for START and END meaning this is a bug, or I am doing something wrong here?

Seriously, dealing with ConstraintLayout programmatically has being really challenging.

like image 469
Eduardo Reis Avatar asked Aug 30 '17 20:08

Eduardo Reis


1 Answers

You should use this instead

set.createHorizontalChain(
            ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,
            ConstraintSet.PARENT_ID, ConstraintSet.LEFT,
            new int[] {ITEM_ID_1, ITEM_ID_2},
            null ,
            ConstraintSet.CHAIN_SPREAD);

This will create a chain between all the items. If your items are created in the XML then you may need to clear the already applied ConstrainSet, by using ConstrainSet.Clear(ITEM_ID) to get a fresh constrainset. Then begin your new constrainset rules.

If you clear the constrainSet for an item, then don't forget to add

set.constrainWidth(ITEM_ID, ConstraintSet.WRAP_CONTENT);
set.constrainHeight(ITEM_ID, ConstraintSet.WRAP_CONTENT);

In last don't forget to .applyTo

set.applyTo(constraintLayout);

Here is a fairly good example of doing this, but this person is overdoing it an connecting all the item beforehand, which isn't necessary. HorizontalChain Example

HorizontalChain

Here is more details about chain styles

Chain Styles

Here is also a Wiki on how to work with constrainlayout in code.

like image 183
Haroun Hajem Avatar answered Nov 15 '22 17:11

Haroun Hajem