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.