16

picture

This view is not constrained, it only has design time positions, so it will jump to (0,0) unless you add constraints

The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with design time attributes (such as layout_editor_absolute X.) These attributes are not applied at run time, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections

pawas kr. singh
  • 416
  • 1
  • 4
  • 12
  • 1
    can you post your layout file here ? – ViramP Dec 18 '17 at 10:08
  • did you copy the text eplaining how it works instead of asking a question? – Stefan Dec 18 '17 at 10:09
  • I think you should start with a quick tutorial of how Constraint Layout works https://www.youtube.com/watch?v=2FOFd77o7eg – Bmbariah Dec 18 '17 at 10:13
  • `To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections` - well that how you can fix it. – Nongthonbam Tonthoi Dec 18 '17 at 10:16
  • I have this same issue: to replicate this, create a new project with empty activity on the wizard and add a text view in the costraintlayout. (AS 3.1.3, and noob android programmer here) – Zac Aug 03 '18 at 05:16

5 Answers5

48

It is very simple to solve this problem. Just click on the widget (for example button or textbox, etc.) then click on the "Infer constraints" Button. You can see in the attached picture or this Youtube link: https://www.youtube.com/watch?v=uOur51u5Nk0

Infer constraints picture

June7
  • 19,874
  • 8
  • 24
  • 34
IoT_Newbie
  • 496
  • 1
  • 5
  • 3
  • when we click on the infer constraints button, that adds the constraints in activity_main.xml. app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" – limonik Mar 05 '20 at 08:20
5

You might have widgets with attributes:

    tools:layout_editor_absoluteX="someValue"
    tools:layout_editor_absoluteY="someValue"

tools namespace is used at development time only, and will be removed while installing apk, so all you layouts might appear at position TOP-LEFT one above other. View Tools Attributes Reference

To fix this: You should make use of Layout Constraints like:

layout_constraintLeft_toLeftOf
layout_constraintLeft_toRightOf
layout_constraintRight_toLeftOf
layout_constraintRight_toRightOf
layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

You can go through the doc of ConstraintLayout and Build a Responsive UI with ConstraintLayout for more details

EDIT:

From the picture you posted, I tried to add appropriate constraints such that the TextView is in Center position using following code:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</android.support.constraint.ConstraintLayout>
Firoz Memon
  • 4,282
  • 3
  • 22
  • 32
0

"Missing constraints in ConstraintsLayout".This error occurs due to the undefined Constraints.To fix this error click on the Widget(ex text,button etc) and then click on the infer constraints which is above the the activity screen.Hope this solves your query

0

enter image description here

First click on Linear layout in Component Tree and then click on infer constraints that is in right side from eye symbol in Activity Screen.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 09 '22 at 11:27
0

In case you do have valid constraints specified for your view, but Android Studio still shoves a "phantom" MissingConstraint error in your face, try these actions:

  1. Clean your project (Build > Clean Project)
  2. Close your project (File > Close Project)
  3. Remove your project's .idea/ directory
    • If you check files from .idea/ into source control, roll them back
  4. Open your project
  5. Rebuild your project (Build > Rebuild Project)

This sacred purification ritual helped me get rid of this "phantom" error, as well as many other weird errors in the past, so I hope it helps you too.

Arthur Khazbs
  • 705
  • 8
  • 19