2

I have this ConstraintLayout:

<android.support.constraint.ConstraintLayout
            android:id="@+id/fragment_live_icons_layout"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fragment_live_clock"
            app:layout_constraintBottom_toTopOf="@+id/fragment_live_progress_bar_time"
            android:background="@color/futbolinAzul"
            android:layout_width="match_parent"
            android:layout_marginLeft="@dimen/margin_n"
            android:layout_marginRight="@dimen/margin_n"
            android:layout_height="@dimen/height_sm"/>

And, programmatically I want to add ImageViews inside it, trying to obtain this picture as the result:

result desired

Where the white rectangle is the ConstraintLayout and the ball and the yellow cards are the different ImageViews added programmatically.

First of all, I created one ImageView:

iconImages = v.findViewById(R.id.fragment_live_icons_layout); //CONSTRAINTLAYOUT
ImageView imageView =  new ImageView(getContext());
imageView.setId(View.generateViewId());
imageView.setBackgroundResource(R.drawable.ic_ball);

After that, I add the imageView to the ConstraintLayout:

iconsImages.addView(imageView);

Then I created a ConstraintSet and i cloned the ConstraintLayout:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(iconImages);

And finally I set the horizontalBias using my variable minutes:

constraintSet.connect(imageView.getId(), ConstraintSet.TOP, iconsImages.getId(), ConstraintSet.TOP, 0);
constraintSet.connect(imageView.getId(), ConstraintSet.LEFT, iconsImages.getId(), ConstraintSet.LEFT, 0);

 //   constraintSet.setVerticalBias(imageView.getId(),  0.5f);
    constraintSet.setHorizontalBias(imageView.getId(),  (float)minutes/90);
    constraintSet.applyTo(iconsImages);
    TransitionManager.beginDelayedTransition(iconsImages);

Where it usually has values in the minutes variable between 15 and 80. The problem is all the imageViews are set at the start o the ConstraintLayout.

Like this: error adding imageViews

Alberto Crespo
  • 2,429
  • 5
  • 28
  • 51

1 Answers1

1

The right constraint seems to be missing. bias must apply both side constraints.

constraintSet.connect(imageView.getId(), ConstraintSet.RIGHT, iconsImages.getId(), ConstraintSet.RIGHT, 0);
pistolcaffe
  • 849
  • 1
  • 8
  • 15