0

Android Studio Emulator do not show the designed layout

I am trying to make my first app in Android but even the my simple app with just 2 Text Fields and 1 Button always float to the corner of the Emulator. I have tried to change the Android Image to x86 or Install HAXM, or tried on another computer with different layout designs but things do not change.

I am a newbie in Android so I hope will get help from you.

Here is my file activity_main.xml

<?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.admin.button.MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Username"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="5dp" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPersonName"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="62dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log In"
        tools:layout_editor_absoluteX="148dp"
        tools:layout_editor_absoluteY="118dp" />
</android.support.constraint.ConstraintLayout>
Dan
  • 45
  • 10

2 Answers2

1

You have not applied any constraints to your views, which force them to be drawn in the top left corner (0,0). Apply appropriate constraints (left, top, right, bottom).

Reason, why you see everything ok in layout editor, is that you've applied hardcoded values via tools: namespace, which will take effect only for layout editor, but not on runtime.

See also this answer which exactly depicts your case.

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • If you know of an existing post that answers the question, please vote as a duplicate, rather than repeating answers across multiple posts. – Mike M. May 05 '17 at 18:18
  • Thank you, finally I got solution from you. – Dan May 06 '17 at 02:39
0

try this

  1. check your manifest file, see if your activity already in the manifest.
  2. check your activity, see if your activity already extend Activity or AppCompatActivity.
Rio A.P
  • 1,313
  • 13
  • 20
  • 1
    I think you didn't understand the question? – Raghavendra May 05 '17 at 04:52
  • thank you for your suggestion, I am posting the file activity_main.xml above so you can check it. – Dan May 05 '17 at 10:49
  • For your suggestions, it has already in the Manifest (because it only has one MainActivity), and it also already extend AppCompatActivity. But though it not works :( – Dan May 05 '17 at 10:52