Q:

How to set the XML attribute 'layout_constrainedWidth' of ConstraintLayout programmatically?

In Constraint Layout, how to convert the xml attribute:
app:layout_constrainedWidth=”true|false”
in code?

A:

I just figured this out.
So base on the answers above,
constrainedWidth can be accessed through ConstraintLayout.LayoutParams and thus can be altered by the following code: (Thank you @JeelVankhede)
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) 
myView.getLayoutParams(); // View for which we need to set constrainedWidth.
lp.constrainedWidth = true/false;
myView.setLayoutParams(lp);

However when using ConstraintsSet the following code would be needed:
val constraintLayout = // your constraint layout
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayout)

// This is the imporatant part
constraint.constrainDefaultWidth(R.id.yourViewId, ConstraintSet.MATCH_CONSTRAINT_WRAP)

constraintSet.applyTo(constraintLayout)

