Set width to match constraints in ConstraintLayout

match_parent is not allowed. But you can actually set width and height to 0dp and set either top and bottom or left and right constraints to “parent”. So for example if you want to have the match_parent constraint on the width of the element, you can do it like this: <TextView android:layout_width=”0dp” android:layout_height=”wrap_content” app:layout_constraintStart_toStartOf=”parent” app:layout_constraintEnd_toEndOf=”parent”/>

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

How to make ConstraintLayout work with percentage values?

It may be useful to have a quick reference here. Placement of views Use a guideline with app:layout_constraintGuide_percent like this: <androidx.constraintlayout.widget.Guideline android:id=”@+id/guideline” android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:orientation=”vertical” app:layout_constraintGuide_percent=”0.5″/> And then you can use this guideline as anchor points for other views. or Use bias with app:layout_constraintHorizontal_bias and/or app:layout_constraintVertical_bias to modify view location when the available space allows … Read more

Differences between ConstraintLayout and RelativeLayout

The intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting. The Rules are similar to RelativeLayout, for example setting the bottom edge to the bottom of some other view. app:layout_constraintBottom_toBottomOf=”@+id/view1″ Unlike RelativeLayout, ConstraintLayout offers a bias value that is used … Read more

ConstraintLayout: change constraints programmatically

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