Samuel
Samuel

Reputation: 1152

ConstraintSet get existing connection

How do get the currently connected view for a given direction from a constraint set?

I'm trying to insert an View before another one

private void insertBefore(int toInsert,int insertBefore)
{
            ConstraintSet cs = new ConstraintSet();

            ConstraintLayout root = (ConstraintLayout)findViewById(R.id.constraintRoot);
            cs.clone(root);

            int currentAbove = cs.getConnectedView(ConstraintSet.TOP);//this is the part I can figure out

            cs.connect(insertBefore,ConstraintSet.TOP,toInsert,ConstraintSet.BOTTOM);
            cs.connect(toInsert,ConstraintSet.TOP,currentAbove,ConstraintSet.BOTTOM);

            root.setConstraintSet(cs);
}

The above is more or less what I am trying to do. I can't find anything in the ConstraintSet Documentation mentioning a function similar to the above function getConnectedView()

I could add an insertAfter parameter to the function but I would rather avoid that.

Here is a diagram of what I want to accomplish enter image description here

Upvotes: 0

Views: 155

Answers (1)

Moisés Cunha
Moisés Cunha

Reputation: 141

See if this works for you

private void insertBefore(@IdRes int toInsert, @IdRes int insertBefore) {
    ConstraintSet cs = new ConstraintSet();
    ConstraintLayout root = (ConstraintLayout) findViewById(R.id.constraintRoot);

    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) findViewById(insertBefore).getLayoutParams();

    cs.clone(root);

    cs.clear(toInsert, ConstraintSet.END);
    cs.clear(toInsert, ConstraintSet.START);

    cs.connect(toInsert, ConstraintSet.TOP, params.topToBottom != -1 ? params.topToBottom : params.topToTop, ConstraintSet.BOTTOM, 0);
    cs.connect(toInsert, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
    cs.connect(toInsert, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);

    cs.connect(insertBefore, ConstraintSet.TOP, toInsert, ConstraintSet.BOTTOM, 0);
    cs.connect(insertBefore, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
    cs.connect(insertBefore, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);

    cs.applyTo(root);
}

Upvotes: 1

Related Questions