0

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"/>
  • try `app:layout_constraintBottom_toTopOf="@+id/first_horizontal_guideline"` in `imageView` and `android:layout_width="0dp"` `android:layout_height="0dp"` in guideline – Abhinav Chauhan Jun 23 '20 at 05:20

4 Answers4

1

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"/>
Mittal Varsani
  • 5,601
  • 2
  • 15
  • 27
1

Use app:layout_constraintHeight_percent to set height according to screen and no need to use a guideline.

Example code

 <ImageView
    android:id="@+id/apartment_main_image_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:src="@drawable/apart"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHeight_percent="0.4"
    app:layout_constraintTop_toTopOf="parent" />
Quick learner
  • 10,632
  • 4
  • 45
  • 55
0

You didn't added the android:orientation="horizontal" attribute use this code

<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.40" />  
0

use layout constraint Height percent to set height and remove the bottom to bottom command or you need 40% of hegith so you can do it by constraint height percentage and make width match parent

For more info and clarification check out the link