Setting constraints programmatically via constraintSet causes views to disappear

542 Views Asked by At

Whenever I am connecting two widgets the starting element will no longer show up. Unfortunately I have no idea what's the cause of this problem. The ConstraintLayout myLayout is already inflated within another layout.

ConstraintLayout myLayout= getActivity().findViewById(R.id.myID);
myLayout.removeAllViews();

ConstraintSet constraintSet = new ConstraintSet();

CheckBox cb = new CheckBox(myLayout.getContext());
cb.setText("CHECKBOX");
cb.setId(View.generateViewId());

myLayout.addView(cb);

constraintSet.connect(cb.getId(),ConstraintSet.END,myLayout.getId(),ConstraintSet.END);
constraintSet.connect(cb.getId(),ConstraintSet.TOP,myLayout.getId(),ConstraintSet.TOP);

constraintSet.applyTo(myLayout);
3

There are 3 best solutions below

0
Harsh On

display after setting the constraints , like this

    ConstraintLayout myLayout= findViewById(R.id.lo1);
    myLayout.removeAllViews();

    ConstraintSet constraintSet = new ConstraintSet();

    CheckBox cb = new CheckBox(myLayout.getContext());
    cb.setText("CHECKBOX");
    cb.setId(View.generateViewId());


    constraintSet.connect(cb.getId(),ConstraintSet.END,myLayout.getId(),ConstraintSet.END);
    constraintSet.connect(cb.getId(),ConstraintSet.TOP,myLayout.getId(),ConstraintSet.TOP);

    constraintSet.applyTo(myLayout);
    myLayout.addView(cb);
0
Cheticamp On

I am not following your two layouts and how they interact but, generally, you would add a view and connect it up as follows:

  1. Create the TextView
  2. Add the view to the layout
  3. Create the ConstraintSet
  4. Clone the layout constraints (and ids) to the ConstraintSet cs.clone(layout)
  5. Make the connections
  6. Apply the ConstraintSet to the layout: constraintSet.applyTo(myLayout)

Also, since you are removing all views from myLayout and not adding anything back to it, your layout is just going to be empty. Fix this then what's above.

0
Moon Cheesez On

There is a certain order which you need to follow when you use ConstraintSet. I'm not too sure why connect only makes the first view disappear but my guess is that something went wrong in trying to set the constraints on the first view.

The proper way to set constraints is:

  1. Add all the views to the constraint layout
  2. clone the constraint layout
  3. Add your constraints with connect
  4. applyTo the constraint layout

Also, you should be using ConstraintSet.PARENT_ID instead of myLayout.getId().

So, shuffling your lines of code and adding the clone:

ConstraintLayout myLayout = getActivity().findViewById(R.id.myID);
myLayout.removeAllViews();

CheckBox cb = new CheckBox(myLayout.getContext());
cb.setText("CHECKBOX");
cb.setId(View.generateViewId());

// 1. Add all the views to the constraint layout
myLayout.addView(cb);

// 2. `clone` the constraint layout
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(myLayout);

// 3. Add your constraints with `connect`
constraintSet.connect(cb.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
constraintSet.connect(cb.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);

// 4. `applyTo` the constraint layout
constraintSet.applyTo(myLayout);

Unfortunately, I have not seen any official documentation on this, this is just what I understand from the ConstraintSet source code and some experimentation.