Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

A view jumps to the top-left because ConstraintLayout has missing constraints

ErrorA view looks right in the preview but sits in the top-left corner on the device

Short answer

Every view in a ConstraintLayout needs at least one horizontal and one vertical constraint. The Layout Editor lets you drop a view anywhere, but on a device an unconstrained view is drawn at the top-left corner. Add the missing constraint, or use Infer Constraints, and replace match_parent with 0dp.

  • Applies to Android Studio, ConstraintLayout 2.2, XML layouts
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    In the layout's Design tab, open Show Warnings and Errors in the toolbar. It lists each view that lacks a horizontal or a vertical constraint.

    grep -n "layout_constraint" app/src/main/res/layout/activity_main.xml
  2. 2Match what you see, then jump to its fix

    1. 1The view is placed where you dropped it in the editor but renders at position 0,0 on the deviceA view has no constraint on one axisFix
    2. 2Many views flagged at once after you moved things around in the Layout EditorYou positioned views by dragging and never connected themFix
    3. 3A view is meant to fill the width or height, but sizing and position are wrongmatch_parent is used on a child of the ConstraintLayoutFix
    4. 4A group of views that should spread out stays bunched up or off placeA chain has an end that is not constrainedFix

What does the error look like?

  • A view looks right in the preview but sits in the top-left corner on the device
  • Missing constraints in ConstraintLayout

Cause 1 of 4

A view has no constraint on one axis

1Check

A view must have at least one horizontal and one vertical constraint. A missing constraint is not a compile error, but the Layout Editor flags it in the toolbar.

grep -n -B2 -A8 "<Button" app/src/main/res/layout/activity_main.xml

2Fix

Constrain the view on both axes. For example, pin it to the top and start of the parent, or place it below another view.

<Button
    android:id="@+id/save"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

3Verify

Run the app, or look at the preview, and confirm the view stays where the constraints put it.

./gradlew :app:installDebug

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 4

You positioned views by dragging and never connected them

1Check

A view dropped into the editor stays where you leave it even without constraints. That is only to make editing easier.

grep -c "layout_constraint" app/src/main/res/layout/activity_main.xml

2Fix

Arrange the views where you want them, then click Infer Constraints in the toolbar. It adds constraints that keep each view at its current position. Check the result on other screen sizes and orientations, since you may need to adjust it.

# Layout Editor toolbar: Infer Constraints
# Optional: enable Autoconnect to Parent before adding views

3Verify

Open Show Warnings and Errors again. The missing-constraint entries are gone.

grep -c "layout_constraint" app/src/main/res/layout/activity_main.xml

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 4

match_parent is used on a child of the ConstraintLayout

1Check

match_parent is not supported for views inside a ConstraintLayout. The documented replacement is match constraints, which is a size of 0dp.

grep -n "match_parent" app/src/main/res/layout/activity_main.xml

2Fix

Set the size to 0dp and constrain that axis on both sides.

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

3Verify

Preview the layout. The view stretches between its two constraints.

grep -n "match_parent" app/src/main/res/layout/activity_main.xml

Confirmed by developer.android.com

Did this fix it?

Cause 4 of 4

A chain has an end that is not constrained

1Check

A chain works only if each end of the chain is constrained to another object on the same axis. A chain also does not align views in the other direction; that needs other constraints.

grep -n "layout_constraintHorizontal_chainStyle\|layout_constraintVertical_chainStyle" app/src/main/res/layout/activity_main.xml

2Fix

Constrain the first view's start side and the last view's end side to the parent, or to a guideline, then add alignment constraints on the other axis.

app:layout_constraintStart_toStartOf="parent"   <!-- first view in the chain -->
app:layout_constraintEnd_toEndOf="parent"       <!-- last view in the chain -->

3Verify

Preview the layout at a narrow and a wide width.

grep -c "layout_constraint" app/src/main/res/layout/activity_main.xml

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Constrain each view as you add it, or turn on Autoconnect to Parent.
  • Treat the toolbar error indicator as a build step: open it before you commit a layout.
  • Preview layouts at more than one screen size before you ship them.
  • Use 0dp, not match_parent, for views that should fill the space between constraints.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android Studio cannot pair the phone over Wi-Fi

The Pair devices over Wi-Fi dialog lists no device

Wireless pairing needs the phone on Android 11 or higher and on the same Wi-Fi network as the computer, with current Platform-Tools.

4 causes, each with a fix

Mobile apps

adb tcpip 5555 fails because no device is found

adb: more than one device/emulator

adb tcpip only works while the device is attached and listed by adb, because it tells the phone to start listening on port 5555.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.