Skip to content Skip to sidebar Skip to footer

Programmatically Connecting Multiple Views Set To Any Size Using ConstraintLayout

I'm using ConstraintLayout beta4 and trying to create a horizontal chain across the screen programmatically. The problem I'm running into is when I connect multiple views together

Solution 1:

There's a couple of problems with what you are doing. First, you are assigning the same LayoutParams instance to both objects -- that won't work. You need a separate instance:

ConstraintLayout.LayoutParams anySizeParams2 = new ConstraintLayout.LayoutParams(0, 0);
rightButton.setLayoutParams(anySizeParams2);

Second, you got the connections wrong:

set.connect(rightButton.getId(),ConstraintSet.LEFT,
            rightButton.getId(),ConstraintSet.RIGHT,0);

should be:

set.connect(rightButton.getId(),ConstraintSet.LEFT,
            leftButton.getId(),ConstraintSet.RIGHT,0);

As otherwise it's not going to create a chain.

It probably would be cleaner to clone the layout after you added the views too.


Post a Comment for "Programmatically Connecting Multiple Views Set To Any Size Using ConstraintLayout"