My app layout is different from activity_main layout design

96 Views Asked by At

I'm trying to make a imperial to metric conversion app, which is going fine, until I ran into problems with the Android Emulator. The layout is totally screwed up in the Emulator, and I have no idea why. https://i.stack.imgur.com/ygiLK.jpg

EDIT:

The whole activity_main.xml code:

<?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.daniel.converter.MainActivity"
    tools:layout_editor_absoluteY="73dp"
    tools:layout_editor_absoluteX="0dp">

    <TextView
        android:id="@+id/textViewMPH"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MPH"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="90dp" />

    <EditText
        android:id="@+id/editTextMPH"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        tools:layout_editor_absoluteX="85dp"
        tools:layout_editor_absoluteY="77dp" />

    <TextView
        android:id="@+id/textViewTO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="132dp" />

    <TextView
        android:id="@+id/textViewKMH"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="KMH"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="168dp" />

    <EditText
        android:id="@+id/editTextKMH"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        tools:layout_editor_absoluteX="85dp"
        tools:layout_editor_absoluteY="156dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Convert!"
        tools:layout_editor_absoluteX="146dp"
        tools:layout_editor_absoluteY="229dp" />

</android.support.constraint.ConstraintLayout>
1

There are 1 best solutions below

0
Sonam On

Issue is happening because you are using constraint layout but you have not provided any constraints for your items. Thus, every item is shown at (0,0) position.

Contraint Layout description:

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.

So either assign x and y values or use LinearLayout/RelativeLayout.