Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why android studio show error of "Missing constraints in constraintlayout"?

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

like image 791
pawas kr. singh Avatar asked Dec 18 '17 10:12

pawas kr. singh


People also ask

How do you fix missing constraints in constraint layout?

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.

What does constraints mean in Android Studio?

The Layout Editor uses constraints to determine the position of a UI element within the layout. A constraint represents a connection or alignment to another view, the parent layout, or an invisible guideline. You can create the constraints manually, as we show later, or automatically using the Autoconnect tool.

Why do we prefer constraint ConstraintLayout in Android?

Advantages of using ConstraintLayout in Android ConstraintLayout provides you the ability to completely design your UI with the drag and drop feature provided by the Android Studio design editor. It helps to improve the UI performance over other layouts.


2 Answers

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

like image 186
IoT_Newbie Avatar answered Sep 20 '22 00:09

IoT_Newbie


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>
like image 21
Firoz Memon Avatar answered Sep 20 '22 00:09

Firoz Memon