User3
User3

Reputation: 2535

Trying to merge two layouts in a custom layout

I am trying to add two xml layouts together by extending a Linear layout, but I am not getting the desired result. When I set this view to a RecyclerView I just get the header row

What might be wrong here?

Both XML layouts I inflate are of type Linear layout,

class AddOnCardView : LinearLayout {

    private var itemData: AddOnCardData? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val header: LinearLayout = inflate(getContext(), R.layout.tw_dashboard_addon_header, this) as LinearLayout
        val childRow = inflate(getContext(), R.layout.tw_dashboard_addon_row, null)
        // prepareChildren(header)

        header.addView(childRow)
    }

    fun setDataItem(itemData: AddOnCardData) {
        this.itemData = itemData

    }


}

Here is the XML for my row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <com.citrus.ui.widget.CustomTextView
            android:id="@+id/money_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="03dp"
            android:gravity="center|end"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:textColor="#FF0099FF"
            android:textSize="15sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="$88.00/mo" />

        <com.citrus.ui.widget.CustomTextView
            android:id="@+id/label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="50dp"
            android:paddingEnd="05dp"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/money_text"
            tools:text="Unlimited Data" />

        <ccom.citrus.ui.widget.CustomTextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="50dp"
            android:paddingEnd="05dp"
            android:textColor="#FF9B9B9B"
            android:textSize="12sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/label"
            tools:text="Get maximum 4G+ speeds" />

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="03dp"
            android:paddingEnd="10dp"
            app:layout_constraintBottom_toBottomOf="@+id/title"
            app:layout_constraintEnd_toEndOf="@+id/money_text" />


    </android.support.constraint.ConstraintLayout>

</LinearLayout>

Here is the XML for my header:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">

        <View
            android:id="@+id/divider_top"
            android:layout_width="match_parent"
            android:layout_height="05dp"
            android:background="@color/gray_light_redesign"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.citrus.ui.widget.CustomTextView
            android:id="@+id/header_text"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center|start"
            android:paddingStart="30dp"
            android:paddingEnd="30dp"
            android:textSize="15sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/divider_top"
            tools:text="ADD-ON-OPTIONS" />


        <!-- @drawable/ic_show_less-->
        <ImageView
            android:id="@+id/expand_collapse"
            android:layout_width="34dp"
            android:layout_height="34dp"
            android:paddingStart="05dp"
            android:paddingEnd="15dp"
            android:src="@drawable/ic_show_more"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <View
            android:id="@+id/divider"
            android:layout_width="match_parent"
            android:layout_height="05dp"
            android:background="@color/gray_light_redesign"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/header_text" />


    </android.support.constraint.ConstraintLayout>
</LinearLayout>

Upvotes: 0

Views: 66

Answers (2)

Quinn
Quinn

Reputation: 9434

It looks like you're not setting the AddOnCardView LinearLayout to have a vertical orientation, and i think it defaults to horizontal... but since your headers width is set to match_parent, the other views have no room.

You can do that by calling setOrientation in your constructor.

Upvotes: 1

Taras Parshenko
Taras Parshenko

Reputation: 604

Instead of doing

val childRow = inflate(getContext(), R.layout.tw_dashboard_addon_row, null)
header.addView(childRow)

You can simply put include into R.layout.tw_dashboard_addon_header:

<include layout="@layout/tw_dashboard_addon_row" />

Upvotes: 0

Related Questions