androidimageviewandroid-constraintlayoutconstraintlayout-guideline

Constraint layout Guideline java.lang.AssertionError: BOTTOM


I have an ImageView and I want it to have height of the 40% of the screen height. I am trying to accomplish it by constraint layout's guidelines, but I get runtime error "java.lang.AssertionError: BOTTOM". I think the error is that image becomes higher than screen's 40%, but I don't know how to fix the bug. here is my xml code.

<ImageView
    android:id="@+id/apartment_main_image_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:src="@drawable/apart"
    app:layout_constraintBottom_toBottomOf="@+id/first_horizontal_guideline"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<androidx.constraintlayout.widget.Guideline
    android:id="@+id/first_horizontal_guideline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintGuide_percent="0.4"/>

Solution

  • This error is because you forgot to add the orientation of the Guideline You can give an orientation of the guideline according to your requirement horizontal or vertical

    When you use a vertical guideline, any view constrained to it should do it horizontally and the same thing for horizontal guidelines.

    <ImageView
        android:id="@+id/apartment_main_image_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@drawable/apart"
        app:layout_constraintBottom_toTopOf="@+id/first_horizontal_guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    
    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/first_horizontal_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.4"/>