how to include same layout multiple times

84 Views Asked by At

Hi I have a layout which I want to use multiple times

but when I use include it only appears once, I looked online but could not get an answer

could you suggest what am I doing wrong here please

thanks in advance R

my common layout

denomination_layout.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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tv_denomination"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:hint="£0.00"
            app:layout_constraintBottom_toBottomOf="@+id/et_denomination_count"
            app:layout_constraintEnd_toStartOf="@+id/et_denomination_count"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/et_denomination_count" />

        <EditText
            android:id="@+id/et_denomination_count"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:hint="0"
            android:inputType="number"
            app:layout_constraintEnd_toStartOf="@+id/tv_denomination"
            app:layout_constraintStart_toEndOf="@+id/tv_denomination_total"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_denomination_total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="£0.00"
            app:layout_constraintBottom_toBottomOf="@+id/et_denomination_count"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/et_denomination_count"
            app:layout_constraintTop_toTopOf="@+id/et_denomination_count" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

fragment.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondFragment">

    <TextView
        android:id="@+id/tv_Cash_in_till"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cash in till"
        android:layout_marginVertical="18dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <include
        android:id="@+id/denomination_fifty"
        layout="@layout/denominations_layout"
        app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till" />

    <include
        android:id="@+id/denomination_twenty"
        layout="@layout/denominations_layout"
        app:layout_constraintTop_toBottomOf="@+id/denomination_fifty" />

    <Button
        android:id="@+id/button_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/previous"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2

There are 2 best solutions below

0
On

You can try to put each inside and set required sizes already on FrameLayout

<FrameLayout
    android:id="@+id/denomination_fifty"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till">

    <include
        layout="@layout/denominations_layout" />
</FrameLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/denomination_fifty">

    <include
        android:id="@+id/denomination_twenty"
        layout="@layout/denominations_layout" />
</FrameLayout>
0
On

To make constraints work, you must add layout_width and layout_height to <include tag.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondFragment">

<TextView
    android:id="@+id/tv_Cash_in_till"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginVertical="18dp"
    android:text="Cash in till"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<include
    android:id="@+id/denomination_fifty"
    layout="@layout/denominations_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/tv_Cash_in_till" />

<include
    android:id="@+id/denomination_twenty"
    layout="@layout/denominations_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@+id/denomination_fifty" />

<Button
    android:id="@+id/button_second"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/previous"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>