Search code examples
androidxmlandroid-linearlayoutandroid-imageviewandroid-cardview

LinearLayout not showing all its children


After creating a LinearLayout with ImageViews as its children, I noticed that only the first row of items are shown. I thought the LinearLayout would automatically wrap its children onto a new line as necessary? The width seems fine but not the height.

Expected result

enter image description here

Expected blueprint (ImageView count is not to scale)

enter image description here For some reason, when I create a LinearLayout inside another view, the width is shown correctly, but it never seems to adjust its height to fit & show all the children inside it.

Current result

enter image description here

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cv_facilities">

    <LinearLayout
        android:id="@+id/ll_facilities"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <LinearLayout
            android:id="@+id/ll_titlerow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_expandcollapsearrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp" />

            <ImageView
                android:id="@+id/iv_topicsymbol"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="10dp" />

            <LinearLayout
                android:id="@+id/ll_symbols"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ImageView
                    android:id="@+id/iv_symbol_a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_language" />

                <ImageView
                    android:id="@+id/iv_symbol_b"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_pets" />

                <ImageView
                    android:id="@+id/iv_symbol_c"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_verified_user" />

                <ImageView
                    android:id="@+id/iv_symbol_d"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_transport" />

                <ImageView
                    android:id="@+id/iv_symbol_e"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_seat" />

                <ImageView
                    android:id="@+id/iv_symbol_f"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_fingerprint" />

                <ImageView
                    android:id="@+id/iv_symbol_g"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_areoplane_depart" />

                <ImageView
                    android:id="@+id/iv_symbol_h"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_areoplane_arrive" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</androidx.cardview.widget.CardView>

Solution

  • This behavior of LinearLayout is "as intended": it will display its children in a horizontal or vertical line.

    Since all of the child Views in your blueprint seem to be of a similar size, consider switching to GridLayout, it is available as androidx-library (e.g. androidx.gridlayout:gridlayout:1.0.0).

    For child Views with varying dimensions, FlexboxLayout is a good alternative. It was introduced in a blog post in February 2017. There is a version for androidx available: 'com.google.android:flexbox:1.1.0'