0

I'm creating a layout_banner.xml that is referenced by multiple other layouts, and has a height of 80dp.

layout_banner.xml:

<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="80dp"
    android:background="#4455ff"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Banner test"
        android:textSize="24sp"
        android:textColor="@color/white"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Preview:

Banner layout

However, once I add this layout into another using <include>, the height isn't the expected value:

activity_main.xml:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include
        android:id="@+id/banner_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/layout_banner"
        app:layout_constraintTop_toTopOf="parent"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/planner_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/banner_layout"
        app:layout_constraintBottom_toBottomOf="parent"
        android:padding="16dp"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

Preview:

enter image description here

Why is the banner shorter than 80dp when referenced with include? The include object has height set to wrap_content so shouldn't layout_banner.xml be able to take up as much height as it needs?


UPDATE: Thanks to the responses so far, removing the width and height from the include layout fixes the size issue, but unfortunately that also makes any constraints be ignored. For example, in the below layout, the banner is 80dp as expected, but should be going below the recyclerView, not over it:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/planner_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="16dp"
        />

    <include
        android:id="@+id/banner_layout"
        layout="@layout/layout_banner"
        app:layout_constraintTop_toBottomOf="@id/planner_recyclerview"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

PM4
  • 624
  • 1
  • 5
  • 18

2 Answers2

2

so that the height of the banner is 80dp remove layout_width and layout_height of include

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • Thanks, that does solve the issue, but it creates another one. When I remove width and height, any constraints are ignored, for example if I try putting the banner below a fixed-height recyclerView. Is there a way to avoid that? – PM4 Mar 12 '21 at 20:43
  • Try to set the height above include and not above the banner – Gonzalo Vivar Mar 12 '21 at 20:54
  • I see, but that defeats some of the purpose of using , as I was looking to define the height in just one place, instead of every time the banner is referenced. However, I suppose I can set the height in dimens.xml and have the blocks use that for height, so I'll accept your answer. Thanks for the help! – PM4 Mar 12 '21 at 21:12
  • 1
    You're welcome! PS: if you use a LinearLayout you will not have this problem – Gonzalo Vivar Mar 12 '21 at 21:23
1

On your activity_main.xml you are overrinding layout_width and layout_height of your layout_banner.xml, just do:

<include
    android:id="@+id/banner_layout"
    layout="@layout/item_agregar_pedido"
    app:layout_constraintTop_toTopOf="parent"
    />
javdromero
  • 1,850
  • 2
  • 11
  • 19
  • Thanks, that does solve the issue, but it creates another one. When I remove width and height, any constraints are ignored, for example if I try putting the banner below a fixed-height recyclerView. Is there a way to avoid that? – PM4 Mar 12 '21 at 20:42
  • Can you post an image of your desired design? Or the edited .xml ? – javdromero Mar 12 '21 at 20:46
  • I have updated the question with a new XML that has the constraints issue. – PM4 Mar 12 '21 at 20:52
  • 1
    From other posts, it seems it's not possible without setting width and height, see [this](https://stackoverflow.com/questions/43676415/how-to-include-constraint-layout-to-another-constraint-layout-and-set-constraint) – javdromero Mar 12 '21 at 21:15