constraintlayout margin not getting applied

424 Views Asked by At

I have a margin applied to a constraintlayout in order for widgets to stop touching edges of the screen, like below

<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/margin_16dp">
    <TextView
        android:id="@+id/lb_top"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Top Right"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/ic_launcher_bg"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_outlined"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:text="Outlined"
        android:textAllCaps="false"
        android:textStyle="bold"
        app:iconTint="@null"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_primary"
        style="@style/Widget.MaterialComponents.Button"
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:text="Primary"
        android:textAllCaps="false"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/btn_outlined"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/btn_outlined"
        app:layout_constraintTop_toBottomOf="@+id/btn_outlined" />

    <TextView
        android:id="@+id/txt_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bottom Bottom Bottom Bottom Bottom Bottom Bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".10" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".45" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".60" />


</androidx.constraintlayout.widget.ConstraintLayout>

Although things show up correctly in design pane, but when I ran the app, there is no margin applied.

I tried to reproduce the issue in a new project but there margin was applying correctly.

What I tried?

  • clean build
  • Invalidate & restart
  • uninstalled previous installation of the app

Any pointers to track down this is highly appreciated!

0

There are 0 best solutions below