109

I get the following error and I am using Android studio 2.2 Preview 3. I searched Google but couldn't find any resources.

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

enter image description here

<TextView
        android:layout_width="384dp"
        android:layout_height="207dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/calc_default_display"
        android:id="@+id/textView"
        android:background="#ffffff"
        android:layout_margin="0dp"
        android:gravity="bottom|end"
        android:textSize="70sp"
        android:nestedScrollingEnabled="false"
        android:maxLines="2"
        android:maxLength="17"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />
Isuru
  • 3,818
  • 13
  • 49
  • 64

12 Answers12

159

To quickly resolve this, use this very helpful shortcut in Android Studio:

Right-click widget-in-question > Constraint Layout > Infer Constraints:

enter image description here

Thereafter, you can tweak the constraints as described here: https://stackoverflow.com/a/37960888/5556250

Update

This is not correct for the Android Studio v3 and up. As per @purpleladydragons's comment:

"Constraint Layout" is not in the dropdown menu. Use the magic wand icon in the toolbar menu above the design preview; there is the "Infer Constraints" button.

Jay Souper
  • 2,576
  • 2
  • 14
  • 17
  • 2
    Thanks a lot for your solution, i am an absolute beginner, i had no idea what was wrong with the widget position – Atul Chavan Mar 26 '17 at 14:25
  • 2
    Great solution although I wish Android Studio would just store the positions from the get go, instead of run time positions. A better question is why Google thought it is a good idea to store position only during run time? That's an unnecessary step to make devs click to infer position. – Antony May 05 '17 at 03:18
  • Thanks a lot! Very helpful – Pavel Geveiler Jun 10 '17 at 14:12
  • 26
    I'm using Android Studio 3.0 - "Constraint Layout" was not in the dropdown menu when I right clicked a widget. I realized there was an icon that looked like a magic wand in the toolbar menu above the design preview, that's also an "Infer Constraints" button. – purpleladydragons Oct 14 '17 at 17:31
  • the solution could not be found in Android Studio Version 3 or up anymore – Mohammad Heydari Mar 13 '18 at 11:25
  • just what i needed. For some reason this happens every time i copy/paste an asset from another fragment. – user2529011 Nov 17 '18 at 02:04
78

Solution

Just click this and it will be solved

Cem U
  • 893
  • 7
  • 14
  • 1
    I don't see that toolbar with that button. How do I find it? – i_am_david Jun 08 '18 at 14:26
  • I dont know if you are using different version of Android Studio. Please check that button, it should be somewhere there. If you cant find , there are other solutions – Cem U Jun 12 '18 at 13:34
13

You need to give a value to the constraints manually when using the new layout editor or the missing constraints can be added automatically by clicking the magic wand button in the toolbar menu of the design preview.

onexf
  • 3,674
  • 3
  • 22
  • 36
11

you can try this: 1. ensure you have added: compile 'com.android.support:design:25.3.1' (maybe you also should add compile 'com.android.support.constraint:constraint-layout:1.0.2') 2. enter image description here

3.click the Infer Constraints, hope it can help you.

BertKing
  • 513
  • 5
  • 13
8

Alright so I know this answer is old, but I found out how to do this for version 3.1.4. So for me this error occurs whenever I put in a new item into the hierarchy, so I knew I needed a solution. After tinkering around for a little bit I found how to do it by following these steps:

  1. Right click the object and go to Center.

Step 1

  1. Then select Horizontally.

Step 2

  1. Repeat those steps, except click Vertically instead of Horizontally.

Provided that that method still works, after step two you should see squiggly lines going horizontally across where the item is, and after step three, both horizontally and vertically. After step three, the error will go away!

tdog
  • 169
  • 2
  • 11
8

From Android Studio v3 and up, Infer Constraint was removed from the dropdown.

Use the magic wand icon in the toolbar menu above the design preview; there is the "Infer Constraints" button. Click on this button, this will automatically add some lines in the text field and the red line will be removed.

enter image description here

Jayavinoth
  • 544
  • 1
  • 9
  • 24
7

Right Click in then designing part on that component in which you got error and follow these steps:

  • [for ex. if error occur in Plain Text]

[1]

Plain Text Constraint Layout > Infer Constraints:

finally error has gone

Omal Perera
  • 2,971
  • 3
  • 21
  • 26
Satyam Mishra
  • 136
  • 1
  • 8
6

Right-click on the widget and choose "center" -> "horizontally". Then choose "center"->"vertically".

Taimoor Hassan
  • 161
  • 2
  • 3
5

You just have to right-click on the widget and choose "center" -> "horizontally" and do it again then choose ->"vertically" This worked for me...

Ekane 3
  • 160
  • 1
  • 12
0

you can go to the XML file then focus your mouse cursor into your button, text view or whatever you choose for your layout, then press Alt + Enter to fix it, after that the error will be gone.it works for me.

Mohammad Heydari
  • 3,933
  • 2
  • 26
  • 33
0

After clicking on the magic wand icon to infer constraints:

1) In Projects window, go to Gradle Scripts > build.gradle (Module:app)

2) Scroll down to dependencies

3) Look for implementation 'com.android.support:appcompat-v7:28.0.0-beta03'

4) Change this line to implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

5) A banner should pop up at the top of the window, click Sync Now in the right corner.

This works for Android Studio v3.1 click for image of edit to build.gradle file

0

When creating a layout, it's easier to work with one control at a time, instead of adding them all at once.

From the Layouts Palette, drag a ConstraintLayout to the screen.

Move your desired controls inside the ConstraintLayout.

So the ConstraintLayout will now be the control's parents, and if you switch to the xml code, the controls will be nested under the ConstraintLayout.

Right click on your control, select Constraint from the menu, and select how you want to align it to the parent ConstraintLayout, top, start, etc.

If you need to align two controls relative to each other, select both controls at the same time with the Ctrl key, then right click to open the constrain menu.

More info: https://developer.android.com/training/constraint-layout

enter image description here

enter image description here You can specify the constraint separation distance in the Constraint Layout tab:

enter image description here

live-love
  • 48,840
  • 22
  • 240
  • 204