ConstraintLayout: How to add multiple views programmatically?

I want to add 2 buttons in ConstraintLayout. My current code is as follows:

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"); layout.addView(button); set.connect(button.getId(), ConstraintSet.LEFT, layout.getId(), ConstraintSet.LEFT, 0); set.connect(button.getId(), ConstraintSet.RIGHT, layout.getId(), ConstraintSet.RIGHT, 0); set.connect(button.getId(), ConstraintSet.BOTTOM, layout.getId(), ConstraintSet.BOTTOM, 0); set.constrainWidth(button.getId(), ConstraintSet.MATCH_CONSTRAINT); 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.LEFT, button.getId(), ConstraintSet.LEFT, 0); set.connect(newButton.getId(), ConstraintSet.RIGHT, button.getId(), ConstraintSet.RIGHT, 0); set.constrainHeight(newButton.getId(), 200); set.applyTo(layout); } 

But I get only 1 visible button (the other is probably hidden behind it), and it is in the upper left corner of the screen. At the bottom of the screen there should be two buttons connected to each other.

What am I doing wrong here?

enter image description here

Desired Result:

enter image description here

+5
source share
1 answer

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 not specified explicitly, all elements will receive the same identifier (-1).

+9
source

Source: https://habr.com/ru/post/1262849/


All Articles