Skip to content
Advertisement

addView adds the view but it’s not showing it

I know this has already been asked, but I tried everything and I couldn’t solve my problem.

When I create the views programmatically, they are definitely added. I checked in the debugger and everything is in it’s place, even the parent view gets bigger in height because they are using space. But I can’t see them. It’s like they are below other views or invisible (but they are not. I checked many times…).

enter image description here

This is the xml code where I’m trying to insert the views. I want to insert them where the cursor is (where it’s tagged information). I only have it there to show you how it will look like in the end, but this part will be added programmatically.

<LinearLayout
            android:id="@+id/llhTestItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp"
            android:orientation="horizontal">

            <TextView
                    android:id="@+id/tvInformationTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="17sp"
                    fontPath="fonts/OpenSans-Regular.ttf"
                    android:text="Sub title: "/> <!-- tvInformationTitle -->

            <TextView
                    android:id="@+id/tvInformation"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="16sp"
                    fontPath="fonts/OpenSans-Light.ttf"
                    android:text="information"/> <!-- tvInformation -->

        </LinearLayout> <!-- information -->

Below you can see the code that I’m using to add the views just like in the xml above.

@Override
public void onBindViewHolder(SetupViewerHolder holder, int position) {
    CardViewItem cardViewItem = cardViewItemList.get(position);
    holder.tvTitle.setText(cardViewItem.getCardTitle());
    for (int i = 0; i < cardViewItem.getInformationList().size(); i++){

        //region Create llhItem
        LinearLayout.LayoutParams llhItemParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        llhItemParams.topMargin = dipToPixels(6);

        LinearLayout llhItem = new LinearLayout(context);
        llhItem.setLayoutParams(llhItemParams);
        llhItem.setOrientation(LinearLayout.HORIZONTAL);
        //endregion

        LinearLayout.LayoutParams tvInformationsParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        //region Create tvInformationTitle
        TextView tvInformationTitle = new TextView(context);
        tvInformationTitle.setLayoutParams(tvInformationsParams);
        tvInformationTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
        if (Build.VERSION.SDK_INT < 23){
            tvInformationTitle.setTextAppearance(context, R.style.OpenSansRegular);
        } else {
            tvInformationTitle.setTextAppearance(R.style.OpenSansRegular);
        }
        tvInformationTitle.setText(cardViewItem.getInformationList().get(i)[0]);
        //endregion

        //region Create tvInformation
        TextView tvInformation = new TextView(context);
        tvInformation.setLayoutParams(tvInformationsParams);
        tvInformation.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        if (Build.VERSION.SDK_INT < 23){
            tvInformation.setTextAppearance(context, R.style.OpenSansLight);
        } else {
            tvInformation.setTextAppearance(R.style.OpenSansLight);
        }
        tvInformation.setText(cardViewItem.getInformationList().get(i)[1]);
        //endregion

        llhItem.addView(tvInformationTitle);
        llhItem.addView(tvInformation);

        holder.llvInformation.addView(llhItem);
    }

Basically what I’m trying to achieve is to have a recycler view, and each item has only one title, one overflow button, but can have multiple information rows. Here is a print of this, which I had hard coded in xml previously as a prototype.

enter image description here

I know of some alternative ways of doing this that might work, but for now I would like to have it like this, since everything is working like it should, the views are just “not visible”.

Advertisement

Answer

Have you tried calling invalidate() after adding the view? Like this:

holder.llvInformation.addView(llhItem);
holder.llvInformation.invalidate();
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement