user1088231
user1088231

Reputation: 31

Android Studio 2.3.1 Button not positioned correctly in emulator or actual device

I selected the simple "Hello World" template added a button "Calculate".

However the image that displays in the emulator and in an actual device does not center the button but instead positions it in the top left corner.

The generated XML shows x, y positioning

Editor View

Emulator View

<Button
    android:id="@+id/Calculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Calculate"
    android:textAppearance="@style/TextAppearance.AppCompat.Display1"
    tools:layout_editor_absoluteX="109dp"
    tools:layout_editor_absoluteY="326dp" />

Upvotes: 1

Views: 1975

Answers (2)

James M
James M

Reputation: 1

If you are not trying to do anything too extensive with positioning objects, go to the "Component Tree" menu > "Constraint Layout" > "Infer Constraints". This will automatically align objects where you position them in the Design terminal and should display correctly on your emulator. *This helped me, using Android 2.3.

Upvotes: 0

user1088231
user1088231

Reputation: 31

Ran Lint and it suggested the emulator/actual view would display differently if there were not layout constraints so I copied the ones from the "Hello World" into the Button and things worked correctly from there on.

<Button
        android:id="@+id/Calculate"
        android:layout_width="154dp"
        android:layout_height="57dp"
        android:text="Calculate"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.224"
        app:layout_constraintVertical_bias="0.938"
         />

Maybe the Design/editor should add them as a default if they needed?

Upvotes: 1

Related Questions