Set width to match constraints in ConstraintLayout
match_parent
is not supported, so use android:layout_width="0dp"
. With 0dp
, you can think of your constraints as 'scalable' rather than 'filling whats left'.
Also, 0dp
can be defined by a position, where match_parent
relies on it's parent for it's position (x,y and width, height)
ConstraintLayout - Match another view's constraints but add margin to make it larger
For the View, set the margins at -16dp
as follows:
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="-16dp"
android:layout_marginTop="-16dp"
android:layout_marginEnd="-16dp"
android:layout_marginBottom="-16dp"
app:layout_constraintBottom_toBottomOf="@id/title"
app:layout_constraintEnd_toEndOf="@id/title"
app:layout_constraintStart_toStartOf="@id/title"
app:layout_constraintTop_toTopOf="@id/title" />
Do not set android:layout_margin="-16dp"
. It will not work.
I am using ConstraintLayout version 2.1.0. Negative margins are not permitted with earlier versions.
In this image, I have rearranged the views and set background colors to highlight the extent of the views.
MATCH_CONSTRAINTS not working in dynamically generated weighted chain
MATCH_CONSTRAINT
is setting the width/height to zero because it is defined as follows in ConstraintSet:
public static final int MATCH_CONSTRAINT = 0;
What this says is that the constraints will define the size. No contraints, or improper constraints, will just result in a zero width/height.
Your code is correct except that the height is set to MATCH_CONSTRAINT
(remember 0 == MATCH_CONSTRAINT
) but you do not apply constraints to the height so the views just disappear (width but no height.) If you supply a set height to the views, they will appear. The following code will show the views with a height of 100px:
private fun createDropTargets(parentView: ConstraintLayout) {
parentView.setOnDragListener(StaffDragListener())
val set = ConstraintSet()
val ids = arrayListOf<Int>()
val weights = arrayListOf<Float>()
for (i in 0..15) {
val drop = View(activity)
drop.setBackgroundColor(Color.RED)
val params = ConstraintLayout.LayoutParams(ConstraintSet.MATCH_CONSTRAINT, 100)
params.setMargins(5, 0, 5, 0)
drop.layoutParams = params
drop.id = View.generateViewId()
parentView.addView(drop)
ids.add(drop.id)
weights.add(1f)
}
set.clone(parentView)
set.createHorizontalChain(ConstraintSet.PARENT_ID, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, ids.toIntArray(), weights.toFloatArray(), ConstraintSet.CHAIN_SPREAD_INSIDE)
set.applyTo(parentView)
}
By the way, the second loop is redundant. All that work is accomplished with the creation of the chain.
You will have to decide if you want a defined height or if you will let the height be set by the appropriate constraints applied in the vertical direction.
Set ConstraintLayout width to match parent width programmatically
In ConstraintLayout in xml when you want a "MATCH_PARENT" width, you have to set the width to 0dp and then set the layout_constraintWidth_default attribute to "spread"
Programmatically you can do the same:
a) set a 0 dp width and height
b) set defaultWidth and defaultHeight constraints
//add the view with 0dp width and height
val layoutParams = ConstraintLayout.LayoutParams(0, 0)
val view = View(context)
view.layoutParams = layoutParams
view.id = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) View.generateViewId() else R.id.yourLayoutId
parent.addView(view)
//apply the default width and height constraints in code
val constraints = ConstraintSet()
constraints.clone(parent)
constraints.constrainDefaultHeight(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD)
constraints.constrainDefaultWidth(view.id, ConstraintSet.MATCH_CONSTRAINT_SPREAD)
constraints.applyTo(parent)
If you need Java:
//add the view with 0dp width and height
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(0, 0);
View view = View(context);
view.setLayoutParams(layoutParams);
view.setId(R.id.yourLayoutId);
parent.addView(view);
//apply the default width and height constraints in code
ConstraintSet constraints = new ConstraintSet();
constraints.clone(parent);
constraints.constrainDefaultHeight(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD);
constraints.constrainDefaultWidth(view.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD);
constraints.applyTo(parent);
Related Topics
Mediarecorder Start Failed: -38
Android Ndk Build, Method Could Not Be Resolved
How to Pause Frame Animation Using Animationdrawable
Android Devices with Different Height Takes Same Layout Folder
Get the Touch Position Inside the Imageview in Android
Android, Move Bitmap Along a Path
Android Volley Self Signed Https Trust Anchor for Certification Path Not Found
Error 12501 Authenticating with Google Sign-In
Android Java.Lang.Illegalstateexception: Couldn't Read Row 0, Col 0 from Cursorwindow
What's the Maximum Size for an Android Shared Preference Value
Android Recyclerview Findviewholderforadapterposition Returns Null
Error Inflating Class and Android.Support.V7.Widget.Cardview
Google Maps API V2 'Failed to Load Map. Could Not Contact Google Servers'
How to Access a File from Asset/Raw Directory
How to Programmatically Get the List of Registered Users in Firebase
Jpeg Images Have Different Pixel Values Across Multiple Devices
What Do I Do About: This Requires Android.Permission.Interact_Across_Users_Full