How to Programmatically Add Views and Constraints to a Constraintlayout

How to programmatically add views and constraints to a ConstraintLayout?

I think you should clone the layout after adding your ImageView.

ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();

ImageView view = new ImageView(this);
view.setId(View.generateViewId()); // cannot set id after add
layout.addView(view,0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);```

Programmatically add constraints to View in constraint layout

Your code order isn't proper, the following code should work.

ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.cLayout);

TextView tv = new TextView(this);
tv.setText("Something");
tv.setId(View.generateViewId());
ConstraintLayout.LayoutParams lp = new ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT, // Width of TextView
ConstraintLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
layout.addView(tv,0);

ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.connect(tv.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START, 8);
set.connect(tv.getId(), ConstraintSet.TOP, editText.getId(), ConstraintSet.BOTTOM, 8);
set.applyTo(layout);

P.S - I agree with Syed Ahmed Jamil that you should preferably use a RecyclerView or ListView, but it doesn't hurt to know how to set ConstraintLayout constraints properly.

Add a view programmatically in ConstraintLayout

To add views to a ConstraintLayout you have to add the constraints using a ConstraintSet.

View v = findViewById(...);
ConstraintLayout cl = (ConstraintLayout) findViewById(...);

ConstraintSet c = new ConstraintSet();
cl.addView(v);
int id = v.getId();

c.clone(cl);
c.connect(id, ConstraintSet.Top, otherViewIdAboveV, ConstraintSet.BOTTOM, 0);
...
other constraints
...
c.applyTo(cl);

Add views inside Constraint Layout and set them full screen PROGRAMMATICALLY

the issue is you didn't set width and height to your image view so by default wrap_content is taken for both. So setting them both 0dp will fix the issue . Add these lines to set the width and height to 0dp.

 mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));

One more thing , instead of getting parent id as constraintLayout.getId() you can use ConstraintSet.PARENT_ID. So final code looks like

 mImage.setLayoutParams(
new LinearLayoutCompat.LayoutParams(
0,0));
constraitLayout.addView(mImage,0);
ConstraintSet set1 = new ConstraintSet();
set1.clone(constraitLayout);
set1.connect(mImage.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set1.connect(mImage.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set1.connect(mImage.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set1.connect(mImage.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set1.applyTo(constraitLayout);

hope this helps

ConstraintLayout: How to add several views programmatically?

Here is the working code of what you want to achieve

  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.activity_main);
ConstraintSet set = new ConstraintSet();
set.clone(layout);

//Button 1:
Button button = new Button(this);
button.setText("Hello");
button.setId(100); // <-- Important
layout.addView(button);
set.connect(button.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set.connect(button.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
set.connect(button.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
set.constrainHeight(button.getId(), 200);
set.applyTo(layout);

//Button 2:
Button newButton = new Button(this);
newButton.setText("Yeeey");
layout.addView(newButton);
set.connect(newButton.getId(), ConstraintSet.BOTTOM, button.getId(), ConstraintSet.TOP, 0);
set.connect(newButton.getId(),ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);
set.connect(newButton.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);
set.constrainHeight(newButton.getId(), 200);
set.applyTo(layout);

}

Important:
If id is not set explicitly, all the elements will get the same id(-1).

How to contrain a view programmatically to parent in ConstraintLayout

From the docs there is a PARENT_ID in the ConstraintSet to use as the ID of constraints.

Also set setHorizontalBias and setVerticalBias methods as well.

The accepted answer of you referenced slashdot question shows you how to use a ConstraintSet

move Constraint Layout programmatically

Instead of layoutLocation.visibility = View.INVISIBLE try layoutLocation.visibility = View.GONE

Constraints are ignored for programmatically added buttons in the constraint layout - Android

You are adding the your button to a different parent. Your 'layout' variable is referenced to parent view. The ConstraintLayout you wanted to add a button to became a child of this view.

Your hierarchy was something like this:

<ConstraintLayout>
<ConstraintLayout>
<Button> --> Added from XML
<ConstraintLayout>
<Button> --> Added from code
</ConstraintLayout>

To solve this you need to take the reference of the constraint layout in XML and add button to it.

For example:

val clMain = layout.findViewById<ConstraintLayout>(R.id.cl_main)
...
clMain.addView(newButton)


Related Topics



Leave a reply



Submit