GoodDay everyone.
Im trying to add a button programmatically to a ConstraintLayout
and set ConstraintSet
to such button.
The problem comes when I try to close the constraintSet from the layout as the button I've added has no ID.
Button button = new Button(this);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(250, ConstraintLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(15,15,15,15);
button.setLayoutParams(params);
button.setText(returnObject.getFunction());
button.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
button.setTextColor(R.drawable.custom_button_color);
button.setBackgroundResource(R.drawable.custom_button);
ConstraintLayout container = findViewById(R.id.highLevelContainer);
container.addView(button);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(container); // Here I get the error
constraintSet.connect(button.getId(), ConstraintSet.END , container.getId(), ConstraintSet.END,0);
constraintSet.connect(button.getId(), ConstraintSet.START, container.getId(),ConstraintSet.START,0);
constraintSet.connect(button.getId(), ConstraintSet.TOP, container.getId(), ConstraintSet.TOP);
constraintSet.applyTo(container);
This is the generated error on the console:
java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet
at android.support.constraint.ConstraintSet.clone
I would like to avoid assigning a random number to the button as ID as it's created in a loop. Any idea how to fix this?