0

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);
MaxxOr
  • 55
  • 2
  • 6
  • hi , please set at least 1 constraint for each axis , here you have defined END , define either TOP or/and BOTTOM as well. – Harsh Mar 19 '20 at 21:48
  • I have added it in question however it does not fix the issue (and/or is relevant). – MaxxOr Mar 19 '20 at 21:51

3 Answers3

0

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);
Harsh
  • 363
  • 4
  • 14
0

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.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
0

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.

Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38