Removing/Adding constraint programmatically in ConstraintLayout
I have not worked through your code, but the following illustrates how to break and make the constraint using ConstraintSet
.
ConstraintSet set = new ConstraintSet();
ConstraintLayout layout;
layout = (ConstraintLayout) findViewById(R.id.layout);
set.clone(layout);
// The following breaks the connection.
set.clear(R.id.bottomText, ConstraintSet.TOP);
// Comment out line above and uncomment line below to make the connection.
// set.connect(R.id.bottomText, ConstraintSet.TOP, R.id.imageView, ConstraintSet.BOTTOM, 0);
set.applyTo(layout);
Another way of doing this is with ConstraintLayout.LayoutParams.UNSET. This way is not using ConstraintSet.clear
.
Assuming we want to remove the bottom constraint of our constraintLayout itself but can be any view:
val containerParams = cl_container.layoutParams as ConstraintLayout.LayoutParams
containerParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET
cl_container.layoutParams = containerParams