Search code examples
androidandroid-layoutandroid-linearlayout

LinearLayout's addView not displaying


I have been trying to add views in my LinearLayout who's orientation is HORIZONTAL.

This LinearLayout is within CoordinatorLayout, NestedScrollView and in the end HorizontalScrollView. This is the structure-

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <mycustom..FlingNestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:orientation="vertical">

            .
            .
            .
            <RelativeLayout
                android:id="@+id/online_size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="20dp"
                android:paddingStart="20dp">

                <mycustom..FontTextView
                    android:id="@+id/lbl_available"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:paddingBottom="20dp"
                    android:paddingTop="20dp"
                    android:text="@string/available_sizes"
                    android:textColor="?android:attr/textColorPrimaryInverse"
                    android:textSize="@dimen/product_detail_available_font_size"
                    app:customFont="@string/font_circular_medium" />

                <HorizontalScrollView
                    android:id="@+id/horizontal_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/lbl_available"
                    android:scrollbars="none">

                    <LinearLayout
                        android:id="@+id/ll_sizes"
                        android:layout_width="match_parent"
                        android:layout_height="7dp"
                        android:orientation="horizontal"
                        android:paddingBottom="@dimen/product_detail_sizes_padding_bottom" />

                </HorizontalScrollView>

            </RelativeLayout>
            .
            .
            .

        </LinearLayout>
    </mycustom..FlingNestedScrollView>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/windowBackground"
        android:fitsSystemWindows="true"
        app:expanded="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="?attr/colorPrimaryDark">
            .
            .
            .


        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>
    .
    .
    .

</android.support.design.widget.CoordinatorLayout>

And in my Activity I'm using following code once I get data in my UI thread -

@BindView(R.id.online_size) View onlineSizes;
@BindView(R.id.ll_sizes) LinearLayout llSizes;

private void displaySizes() {
        onlineSizes.setVisibility(View.VISIBLE);
        if (llSizes.getChildCount() <= 0) {
            for (int i = 0; i < mPresenter.getSizeArrayList().size(); i++) {
                FontTextView label = (FontTextView) LayoutInflater.from(getActivityContext()).inflate(R.layout.listitem_label_size_element, null);
                label.setText(mPresenter.getSizeArrayList().get(i));
                View view = LayoutInflater.from(getActivityContext()).inflate(R.layout.listitem_image_size_element, null);
                llSizes.addView(label);
                if (i != mPresenter.getSizeArrayList().size() - 1)
                    llSizes.addView(view);
            }
        }
    }

The inner layouts are listitem_label_size_element.xml

<?xml version="1.0" encoding="utf-8"?>
<mycustom..FontTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/lbl_size"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginEnd="@dimen/product_detail_sizes_intermediate_padding_right"
    android:layout_marginRight="@dimen/product_detail_sizes_intermediate_padding_right"
    android:gravity="center_vertical"
    android:textColor="?android:attr/textColorSecondary"
    android:textSize="@dimen/product_detail_sizes_font_size"
    app:customFont="@string/font_ss_regular" />

And listitem_image_size_element.xml is

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/img_dot"
    android:layout_width="5dp"
    android:layout_height="5dp"
    android:layout_gravity="center_vertical"
    android:layout_marginEnd="@dimen/product_detail_sizes_intermediate_padding_right"
    android:layout_marginRight="@dimen/product_detail_sizes_intermediate_padding_right"
    android:adjustViewBounds="true"
    android:gravity="center_vertical"
    android:scaleType="fitXY"
    android:src="@drawable/dot" />

After getting data, I debugged the code and checked llSizes has 10 mChildCount but still it is not displaying.

PS: Before putting it into Duplicate thread, I have tested following things-

llSize.invalidate();

llSize.notify();

LayoutInflater.from(getActivityContext()).inflate(R.layout.listitem_label_size_element, llSizes, false);

LayoutInflater.from(getActivityContext()).inflate(R.layout.listitem_image_size_element, llSizes, false);

Kept whole method displaySizes() into llSizes.postDelayed() with 100 milliseconds delay.

Now apart from this I am not sure what I am missing out.


Solution

  • It's because you have set your layout height to 7dp. Make it wrap_content and set a min or max height for it and you are go.