0

I would like to add some constraints to a TextView within a ConstraintLayout programmatically. I know that I had to use ConstraintSet, but I have no idea how to add the xml attribute: layout_constraintRight_toLeftOf (and its equivalencies) on that textView in Java in android.

Any help is appreciated!!

I. A
  • 2,252
  • 26
  • 65
  • Possible duplicate of [Android : How to programatically set layout\_constraintRight\_toRightOf "parent"](https://stackoverflow.com/questions/41670618/android-how-to-programatically-set-layout-constraintright-torightof-parent) – Rapunzel Van Winkle Aug 24 '17 at 00:50

1 Answers1

1

You can set like this,

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout); // root layout

// If you want to align parent to the layout left
constraintSet.connect(textView.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT);

// If you want to align to the left of one more textview - second text view
constraintSet.connect(textView.getId(), ConstraintSet.RIGHT, textView1.getId(), ConstraintSet.LEFT);

/*______________________________
|      Text1     Text2       |
|                            |*/
constraintSet.applyTo(layout);
Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41