How to access parent in ConstraintLayout

You can use ConstraintSet's Connect method to perform such operations. There PARENT_ID to refer parent id.

ConstraintLayout layout = (ConstraintLayout)fndViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
//view refers to the view's constraint to be changed
set.applyTo(layout);

connect

void connect (int startID, int startSide, int endID, int endSide, int margin)

Create a constraint between two widgets.


You can use PARENT_ID

so your code becomes

params.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
params.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;