(#) Missing Constraints in ConstraintLayout !!! ERROR: Missing Constraints in ConstraintLayout This is an error. Id : `MissingConstraints` Summary : Missing Constraints in ConstraintLayout Severity : Error Category : Correctness Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : 2.2.0 (September 2016) Affects : Resource files Editing : This check runs on the fly in the IDE editor Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/ConstraintLayoutDetector.kt) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ConstraintLayoutDetectorTest.kt) The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as `layout_editor_absoluteX`). These attributes are **not** applied at runtime, 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. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text res/layout/layout1.xml:11:Error: This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints [MissingConstraints] <TextView -------- res/layout/layout1.xml:16:Error: This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints [MissingConstraints] <TextView -------- res/layout/layout1.xml:38:Error: This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint [MissingConstraints] <TextView -------- res/layout/layout1.xml:47:Error: This view is not constrained horizontally: at runtime it will jump to the left unless you add a horizontal constraint [MissingConstraints] <TextView -------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `res/layout/layout1.xml`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers <androidx.constraintlayout.widget.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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="81dp" tools:context="test.pkg.myapplication.MainActivity" tools:ignore="HardcodedText"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Not constrained and no designtime positions" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Not constrained" tools:layout_editor_absoluteX="21dp" tools:layout_editor_absoluteY="23dp" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Constrained both" app:layout_constraintBottom_creator="2" app:layout_constraintBottom_toBottomOf="@+id/activity_main" app:layout_constraintLeft_creator="2" app:layout_constraintLeft_toLeftOf="@+id/activity_main" app:layout_constraintRight_creator="2" app:layout_constraintRight_toRightOf="@+id/activity_main" app:layout_constraintTop_creator="2" app:layout_constraintTop_toTopOf="@+id/activity_main" tools:layout_editor_absoluteX="139dp" tools:layout_editor_absoluteY="247dp" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Constrained Horizontally" app:layout_constraintLeft_creator="0" app:layout_constraintLeft_toLeftOf="@+id/textView3" tools:layout_editor_absoluteX="139dp" tools:layout_editor_absoluteY="270dp" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Constrained Vertically" app:layout_constraintBaseline_creator="2" app:layout_constraintBaseline_toBaselineOf="@+id/textView4" tools:layout_editor_absoluteX="306dp" tools:layout_editor_absoluteY="270dp" /> <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/androidx.constraintlayout.widget.Guideline" app:orientation="vertical" tools:layout_editor_absoluteX="20dp" tools:layout_editor_absoluteY="0dp" app:relativeBegin="20dp" /> <requestFocus/> </androidx.constraintlayout.widget.ConstraintLayout> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/ConstraintLayoutDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `ConstraintLayoutDetector.testMissingConstraints`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Adding the suppression attribute `tools:ignore="MissingConstraints"` on the problematic XML element (or one of its enclosing elements). You may also need to add the following namespace declaration on the root element in the XML file if it's not already there: `xmlns:tools="http://schemas.android.com/tools"`. ```xml <?xml version="1.0" encoding="UTF-8"?> <android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingConstraints" ...> ... </android.support.constraint.ConstraintLayout> ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="MissingConstraints" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'MissingConstraints' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore MissingConstraints ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).