Constraintlayout: Change Constraints Programmatically

ConstraintLayout: change constraints programmatically

  1. To set constraints of image view to:

     app:layout_constraintRight_toRightOf="@+id/check_answer1"
    app:layout_constraintTop_toTopOf="@+id/check_answer1"

    use:

     ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);
    constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer1,ConstraintSet.RIGHT,0);
    constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer1,ConstraintSet.TOP,0);
    constraintSet.applyTo(constraintLayout);
  2. To set constraints of image view to:

     app:layout_constraintRight_toRightOf="@+id/check_answer2"
    app:layout_constraintTop_toTopOf="@+id/check_answer2"

    use:

     ConstraintLayout constraintLayout = findViewById(R.id.parent_layout);
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);
    constraintSet.connect(R.id.imageView,ConstraintSet.RIGHT,R.id.check_answer2,ConstraintSet.RIGHT,0);
    constraintSet.connect(R.id.imageView,ConstraintSet.TOP,R.id.check_answer2,ConstraintSet.TOP,0);
    constraintSet.applyTo(constraintLayout);

How can i change constraint programmatically

First add:

android:id="@+id/constraint_layout"

to your ConstraintLayout, then change:

android:onClick="on_expand"

in:

android:onClick="onExpand"

to respect lowerCamelCase methods naming convention.

Finally this should be your code:

public class MainActivity extends AppCompatActivity {

private ConstraintLayout mConstraintLayout;
private Button mButton;
private ConstraintSet mConstraintSet = new ConstraintSet();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = findViewById(R.id.constraint_layout);
mButton = findViewById(R.id.button2);
}

public void onExpand(View view) {
// If the button is already visible then you don't need to apply any constraints
if (mButton.getVisibility() == View.VISIBLE) {
return;
}
/* Otherwise:
- Make the button2 visible;
- Insert the actual constraints in the ConstraintSet
- Define a new constraint between button3 and button2 (TopToBottomOf)
- Apply it to the ConstraintLayout */
mButton.setVisibility(View.VISIBLE);
mConstraintSet.clone(mConstraintLayout);
mConstraintSet.connect(R.id.button3, ConstraintSet.TOP,
R.id.button2, ConstraintSet.BOTTOM);
mConstraintSet.applyTo(mConstraintLayout);
}
}

How to contrain a view programmatically to parent in ConstraintLayout

From the docs there is a PARENT_ID in the ConstraintSet to use as the ID of constraints.

Also set setHorizontalBias and setVerticalBias methods as well.

The accepted answer of you referenced slashdot question shows you how to use a ConstraintSet

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);

move Constraint Layout programmatically

Instead of layoutLocation.visibility = View.INVISIBLE try layoutLocation.visibility = View.GONE

How to programmatically add views and constraints to a ConstraintLayout?

I think you should clone the layout after adding your ImageView.

ConstraintLayout layout = (ConstraintLayout)findViewById(R.id.mainConstraint);
ConstraintSet set = new ConstraintSet();

ImageView view = new ImageView(this);
view.setId(View.generateViewId()); // cannot set id after add
layout.addView(view,0);
set.clone(layout);
set.connect(view.getId(), ConstraintSet.TOP, layout.getId(), ConstraintSet.TOP, 60);
set.applyTo(layout);```


Related Topics



Leave a reply



Submit