Items are in wrong place in Android Studio emulator

15,895

Solution 1

Do you absolutely need to use constraint layout? If not, you can use a relative layout, and use layout_centerHorizontal and layout_centerVertical like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="blah blah"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="press"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/edittext"
        />

</RelativeLayout>

Solution 2

Select Component/Item(such as BUTTON) in activity_main.xml -> click on "Infer Constrains(as I show in attached file)"

Your item will take right place in ASE

Do same for the rest Component/Items

Enjoy

Share:
15,895
A. Sharkh
Author by

A. Sharkh

Updated on June 04, 2022

Comments

  • A. Sharkh
    A. Sharkh almost 2 years

    I'm new to android studio and this is my first app. It works correctly but having an issue with the emulator, it shows the button in the wrong place.

    <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.dell_pc.myapplication.MainActivity">
    
        <EditText
            android:id="@+id/txtyear"
            android:layout_width="193dp"
            android:layout_height="60dp"
            android:ems="10"
            android:hint="Enter year of birth"
            android:inputType="number"
            tools:layout_editor_absoluteY="129dp"
            tools:layout_editor_absoluteX="96dp" />
    
        <Button
            android:id="@+id/butage"
            android:layout_width="115dp"
            android:layout_height="41dp"
            android:onClick="BuGetAge"
            android:text="Show Age"
            tools:layout_editor_absoluteY="277dp"
            tools:layout_editor_absoluteX="135dp" />
    
    </android.support.constraint.ConstraintLayout>
    

    How can I make it to appear in the right place?

    picture of the emulator