A view jumps to the top-left because ConstraintLayout has missing constraints
A view looks right in the preview but sits in the top-left corner on the deviceShort 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.
Fix it fast
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.xml2Match what you see, then jump to its fix
- 1
The 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
Many views flagged at once after you moved things around in the Layout EditorYou positioned views by dragging and never connected themFix - 3
A 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
A group of views that should spread out stays bunched up or off placeA chain has an end that is not constrainedFix
- 1
What does the error look like?
A view looks right in the preview but sits in the top-left corner on the deviceMissing 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.xml2Fix
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:installDebugConfirmed by developer.android.com
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.xml2Fix
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 views3Verify
Open Show Warnings and Errors again. The missing-constraint entries are gone.
grep -c "layout_constraint" app/src/main/res/layout/activity_main.xmlConfirmed by developer.android.com
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.xml2Fix
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.xmlConfirmed by developer.android.com
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.xml2Fix
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.xmlConfirmed by developer.android.com
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
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fixMobile apps
The phone shows as unauthorized and Android Studio will not run the app
adb devices lists the phone but Android Studio cannot deploy to it
An unauthorized phone means it has not trusted this computer's RSA key.
3 causes, each with a fixMobile 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 fixMobile apps
adb cannot connect to its daemon or start the server
* daemon started successfully *
The adb server is a background process on your computer that listens on TCP port 5037.
2 causes, each with a fixMobile apps
adb prints 'server version doesn't match this client; killing...'
adb server version doesn't match this client; killing...
adb runs a background server, and every adb command is a client.
3 causes, each with a fixMobile 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