Alfonsus Dhani
Alfonsus Dhani

Reputation: 29

Layout in Emulator Different With Layout in Designer

I code using Android Studio. I found some problem that layout in emulator and APK (Build APK and run in my gadget) is different with layout when I design in deisgner. What is wrong with my code ? Or is there something I must set ?

Layout in designer : enter image description here

Layout in gadget (apk) : enter image description here

My XML Code :

<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.alfonsus.ebaandroid.Login">

<EditText
    android:id="@+id/etPassword"
    android:layout_width="280dp"
    android:layout_height="41dp"
    android:ems="10"
    android:hint="Password"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="52dp"
    tools:layout_editor_absoluteY="113dp" />

<EditText
    android:id="@+id/etUserId"
    android:layout_width="280dp"
    android:layout_height="41dp"
    android:ems="10"
    android:hint="User ID"
    android:inputType="textPersonName"
    tools:layout_editor_absoluteX="52dp"
    tools:layout_editor_absoluteY="44dp" />

<Button
    android:id="@+id/btnLogin"
    android:layout_width="143dp"
    android:layout_height="47dp"
    android:text="Login"
    tools:layout_editor_absoluteX="121dp"
    tools:layout_editor_absoluteY="194dp" />

Upvotes: 0

Views: 93

Answers (3)

Ben P.
Ben P.

Reputation: 54204

Each view in your layout has attributes like these:

tools:layout_editor_absoluteX="52dp"
tools:layout_editor_absoluteY="113dp"

Any tools: attribute will only affect what you see in the layout/design preview, and will have no effect on what you see when you actually run your app on an emulator or real device.

In your case, these were added when you dragged-and-dropped views into the visual editor (in order to make your life easy; otherwise the views would snap to the top-left corner like you're seeing on a real device). However, you still need to add constraints to each view so that they'll appear correctly when you run your app.

See this link for more information: https://developer.android.com/training/constraint-layout/#add-a-constraint

Note that you must follow item #3:

  1. Do one of the following:

    • Click a constraint handle and drag it to an available anchor point (the edge of another view, the edge of the layout, or a guideline).

    • Click Create a connection in the view inspector at the top of the Attributes window.

Upvotes: 1

James
James

Reputation: 1972

You need to make each item have a constraint.

Upvotes: 0

The problem here is that you are missing some constraints.

Check your android editor/designer layout again, I think you are missing constraints. Every view in android must have a vertical and a horizontal constraint.

Upvotes: 0

Related Questions