Android Studio message: Missing Constraints in ConstraintLayout

1

It was not until recently that I learned in Java how to program applications on Android and I am practically a novice in this technology, it would help me a lot if you helped me with this warning message.

  

Message: This view is not constrained, it only has designtime   positions, so it will jump to (0,0) unless you add constraints   Suggested Fixes:

     

- Suppress: Add tools: ignore="MissingConstraints" attribute    Priority: 6/10 Category: Correctness Severity: Error Explanation:   Missing Constraints in ConstraintLayout. 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 to widget has both   horizontal and vertical constraints by dragging from the edge   connections.

I would do what the warning tells me but I would not understand what I just did.

    
asked by Cokóro R1 01.07.2017 в 01:45
source

1 answer

2

You are using support for ConstraintLayout , if you do not add restrictions, when you start your application, all the views will move to the coordinate 0,0 (top right view).

You have two options:

  • You add restrictions to your views.

  • Change the ConstraintLayout in your layout to another layout, for example LinearLayout or RelativeLayout .

Review these related questions:

Elements stacked Android Studio

Is it possible to add a LinearLayout in a ConstraintLayout or any other Layout?

How to handle the alignment of View elements in Constraint Layout?

About ConstraintLayout , I recommend you read first link

As an example, a layot containing 2 views (buttons):

<Button
    android:id="@+id/Boton1"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:text="Entrada"
    android:textSize="30sp" />

<Button
    android:id="@+id/Boton2"
    android:layout_width="147dp"
    android:layout_height="56dp"
    android:text="Boton 2"
    android:textSize="30sp" />

Later you can add a margin for example left (left) and top (top) to position the buttons, this is done by clicking and dragging one of the green circles that is on the left, right, up and down the sight.

You would get a layout similar to:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.pedro.botones.Botones">

    <Button
        android:id="@+id/Boton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Boton 1"
        android:textSize="30sp"
        android:layout_marginLeft="119dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="49dp" />

    <Button
        android:id="@+id/Boton2"
        android:layout_width="wrap_content"
        android:layout_height="56dp"
        android:text="Boton 2"
        android:textSize="30sp"
        android:layout_marginLeft="119dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="206dp" />

</android.support.constraint.ConstraintLayout>

When you run your application you can see the buttons according to the positions you defined in the elements within the ConstraintLayout. It is important to read the theory and use of the tool.

    
answered by 01.07.2017 / 06:24
source