This view is not constrained
Asked Answered
D

12

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" />
Demogorgon answered 14/6, 2016 at 16:31 Comment(0)
E
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://mcmap.net/q/157463/-this-view-is-not-constrained-vertically-at-runtime-it-will-jump-to-the-left-unless-you-add-a-vertical-constraint

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.

Eyeglasses answered 16/3, 2017 at 13:40 Comment(6)
Thanks a lot for your solution, i am an absolute beginner, i had no idea what was wrong with the widget positionKarame
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.Helton
Thanks a lot! Very helpfulHorvitz
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.Polarimeter
the solution could not be found in Android Studio Version 3 or up anymorePortemonnaie
just what i needed. For some reason this happens every time i copy/paste an asset from another fragment.Unmeriting
S
78

Solution

Just click this and it will be solved

Samba answered 9/11, 2017 at 12:42 Comment(3)
I don't see that toolbar with that button. How do I find it?Atingle
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 solutionsSamba
This helped me in 2024!Cassidycassie
Y
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.

Year answered 22/6, 2016 at 8:25 Comment(0)
P
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.

Patience answered 15/9, 2017 at 2:43 Comment(0)
P
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!

Pessimist answered 19/8, 2018 at 18:32 Comment(0)
E
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

Editor answered 12/6, 2019 at 17:9 Comment(0)
J
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

Jamnis answered 17/6, 2017 at 11:36 Comment(0)
O
6

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

Outstation answered 25/2, 2019 at 19:57 Comment(0)
S
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...

Snigger answered 6/9, 2018 at 13:39 Comment(0)
P
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.

Portemonnaie answered 13/3, 2018 at 11:34 Comment(0)
W
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

Wrier answered 4/8, 2018 at 21:12 Comment(0)
C
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

Cytokinesis answered 28/3, 2020 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.