Gabriel Gavrilov
Gabriel Gavrilov

Reputation: 361

How come my widgets aren't positioned properly when I run my app?

I'm really new to Android Studio, so apologies in advance if this is a silly question. I'm trying to make a simple app that the text changes when you press the button, but whenever I run the app in the emulator the positions are different than the ones that are shown in android's widget design.

Here is how I want it to look like

Here is how it looks like

xml file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Text"
        android:textSize="20sp"
        tools:layout_editor_absoluteX="112dp"
        tools:layout_editor_absoluteY="399dp" />

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:textColor="#000000"
        android:textSize="50sp"
        tools:layout_editor_absoluteX="158dp"
        tools:layout_editor_absoluteY="320dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

If somebody could help, that would be greatly appreciated, thanks in advance!

Upvotes: 0

Views: 113

Answers (1)

Syed Irfan Hussaini
Syed Irfan Hussaini

Reputation: 473

You should not use tool property in your widgets instead you should use constraints

Replace your code with this code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change Text"
    android:textSize="20sp"
    android:layout_marginTop="22dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txt" />

<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:textColor="#000000"
    android:textSize="50sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Related Questions