Search code examples
androidlayoutpreview

Layout is fine in preview, but is messed up in virtual/real device


Here are the pictures and xml file. It looks fine in the preview, but when i run it on virtual device or a real device, the layout gets messed up. Ive tried deleting everything and doing it again, but the layout still gets messed up.

The code below is for the bottom half of the layout.

Virtual Device virtual device Preview preview

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintLayout2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/calcBtn"
    app:layout_constraintVertical_bias="0.0">

    <TextView
        android:id="@+id/maltReq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lekton_reg"
        android:text="0.00"
        android:textColor="@android:color/background_light"
        android:textSize="60sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/maltReqTV" />

    <TextView
        android:id="@+id/maltReqTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="65dp"
        android:fontFamily="@font/opensans_regular"
        android:text="Malt Required"
        android:textColor="@android:color/background_light"
        android:textSize="15sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/lbBtn"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginBottom="41dp"
        android:layout_marginEnd="60dp"
        android:layout_marginTop="112dp"
        android:background="@drawable/unit_btn_unclicked"
        android:fontFamily="@font/lekton_reg"
        android:text="LB"
        android:textColor="@android:color/background_light"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/maltBillBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/kgBtn"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/kgBtn"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginBottom="41dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="112dp"
        android:background="@drawable/unit_btn_clicked"
        android:fontFamily="@font/lekton_reg"
        android:text="KG"
        android:textColor="@android:color/background_light"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@+id/maltBillBtn"
        app:layout_constraintEnd_toStartOf="@+id/lbBtn"
        app:layout_constraintStart_toEndOf="@+id/maltReq"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/maltBillBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/btn_bg_flat"
        android:drawablePadding="5dp"

       android:drawableRight="@drawable/ic_keyboard_arrow_right_black_24dp4"
        android:fontFamily="@font/open_sans_light"
        android:text="MALT BILL"
        android:textColor="@android:color/background_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

'


Solution

  • Your problem is that your buttons are set position relative to your bottom button - it looks good in the emulator because you're looking at one specific size. If your intent is that it is always aligned with @id/maltReq, aligning your buttons to be BottomToBottom with maltReq would be a better responsive layout than anchoring them at the top of your button on the bottom of the screen.