Error to show an Activity, elements stacked position 0,0

1

Good morning, I am starting to work with the AndroidStudio tool (I am a total beginner) and I have a small "error" (I really do not know what it is). My design is like this:

But when emulating the application, it is shown in this way:

The elements are on top of each other ... I also put the code xml of it:

<?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:paddingLeft="14dp"
    android:paddingRight="14dp"
    android:paddingTop="14dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="andresk21.com.proyecto.MainActivity"
    android:weightSum="1">

    <TextView
        android:id="@+id/lblTitulo"
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:text="@string/titulo"
        android:gravity="center_horizontal"
        android:paddingBottom="14dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/txtPaquete"
        android:paddingLeft="14dp"
        android:paddingRight="14dp"
        android:paddingTop="14dp"
        android:paddingBottom="14dp"
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:hint="@string/numero_paquete"
        android:gravity="center_horizontal"
        android:inputType="number"
        tools:layout_editor_absoluteY="50dp"
        tools:layout_editor_absoluteX="8dp"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/btnBuscar"
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:paddingTop="14dp"
        android:text="@string/boton_buscar"
        android:textColor="@android:color/background_light"
        tools:ignore="MissingConstraints"
        android:onClick="llamarSegundoActiviy"
        tools:layout_editor_absoluteX="7dp"
        tools:layout_editor_absoluteY="121dp" />


</android.support.constraint.ConstraintLayout>

I insist that I am new to this tool (in case the solution is simple for most of you) ... In advance, thank you.

    
asked by Andres Henriquez 12.09.2017 в 05:41
source

2 answers

1

It's not really a problem that you have ConstraintLayout support, and these using as main layout a ConstraintLayout , I suggest you check this information on the site:

What's new in ConstraintLayout or some basic differences with the other layouts?

Elements stacked Android Studio

Android Studio message: Missing Constraints in ConstraintLayout

As a solution, if you do not want to use ConstraintLayout , simply change in your layout .xml the layout type , for example from:

<android.support.constraint.ConstraintLayout 

to another layout type for example LinearLayout :

<LinearLayout 

or RelativeLayout :

   <RelativeLayout

If you want to use ConstraintLayout , then you must define restrictions on your views so that they appear in the correct position when your application is executed, since in design they can be displayed correctly but when you start your application all the views are shown in the same position:

    
answered by 12.09.2017 / 16:43
source
2

You could make some changes in your layout to solve the problem and also simplify the code:

Instead of using tools:layout_editor_absoluteX , use app:layout_constraintRight_toRightOf and app:layout_constraintLeft_toLeftOf to set the position of each element on the axis x relative to other elements instead of using fixed values.

Instead of using tools:layout_editor_absoluteY , use app:layout_constraintTop_toBottomOf and android:layout_marginTop to set the position of each element on the axis y relative to other elements instead of using fixed values.

You can delete the elements related to padding and tools:ignore="MissingConstraints" since they do not significantly affect the way in which the layout will be displayed

The layout could look like this:

<?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="andresk21.com.proyecto.MainActivity">

    <TextView
        android:id="@+id/lblTitulo"
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:text="@string/titulo"
        android:gravity="center_horizontal"
        android:layout_marginTop="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/txtPaquete"
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:hint="@string/numero_paquete"
        android:gravity="center_horizontal"
        android:inputType="number"
        android:layout_marginTop="22dp"
        app:layout_constraintTop_toBottomOf="@+id/lblTitulo"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <Button
        android:id="@+id/btnBuscar"
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="@string/boton_buscar"
        android:textColor="@android:color/background_light"
        android:onClick="llamarSegundoActiviy"
        android:layout_marginTop="22dp"
        app:layout_constraintTop_toBottomOf="@+id/txtPaquete"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout>
    
answered by 12.09.2017 в 07:44