1 votes

Android Studio mensaje: Missing Constraints in ConstraintLayout

It wasn't until recently that I learned in Java what is necessary to program Android applications and I am practically a novice in this technology, it would help me a lot if you could help 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 a 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.

3voto

Elenasys Points 67941

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

You have two options:

  • You add restrictions to your views.

  • Change in your layout the ConstraintLayout by another layout e.g. LinearLayout o RelativeLayout .

Check out these related questions:

Android Studio stacked elements

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 the ConstraintLayout I recommend that you first read https://developer.Android.com/training/constraint-layout/index.html

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" />

Then you can add a margin for example left and top to position the buttons, this is done by clicking and dragging any of the green circles on the left, right, top and bottom of the view.

introducir la descripción de la imagen aquí

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 will see the buttons according to the positions you defined in the elements inside the ConstraintLayout. It is important to read the theory and the use of the tool.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X