2

I want to hide a LinearLayout and all its subviews without taking any space. Normally GONE property should word but this is not the case. I have a LinearLayout containing children but if I apply adUnitLayout.setVisibility(View.GONE); then I still have a blank space on my screen (look at * on screen below).

enter image description here

I want to hide the first LinearLayout (@+id/adUnit) and all its subviews.

But with code below it works.

LinearLayout adUnitLayout = (LinearLayout)itemView;

for(int i=0; i < adUnitLayout.getChildCount(); i++) {
    adUnitLayout.getChildAt(i).setVisibility(View.GONE);
}

Why a simple adUnitLayout.setVisibility(View.GONE); doesn't work ?

Here is my layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adUnit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@drawable/item_border_shadow">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp">

        <ImageView
            android:id="@+id/nativeAdIcon"
            android:layout_width="50dp"
            android:layout_height="50dp" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp">

            <TextView
                android:id="@+id/nativeAdTitle"
                android:lines="1"
                android:ellipsize="end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/nativeAdBody"
                android:lines="2"
                android:ellipsize="end"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"
                android:textColor="@android:color/black"/>

        </LinearLayout>

    </LinearLayout>

    <ImageView
        android:id="@+id/nativeAdImage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:orientation="horizontal"
        android:weightSum="5">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RatingBar
                android:id="@+id/nativeAdStarRating"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="?android:attr/ratingBarStyleSmall"
                android:stepSize="0.1"
                android:visibility="gone"
                android:isIndicator="true" />

            <TextView
                android:id="@+id/nativeAdSocialContext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:lines="2"
                android:ellipsize="end"
                android:textSize="15sp"
                android:textColor="@android:color/black" />

        </LinearLayout>

        <Button
            android:id="@+id/nativeAdCallToAction"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="16sp"
            android:visibility="gone" />
    </LinearLayout>

</LinearLayout>
guillaume
  • 1,638
  • 5
  • 24
  • 43
  • 1
    Welcome to Stack Overflow! I indented your code sample by 4 spaces so that it renders properly. Please edit your post and specify which linear layout you are trying to hide because it is not clear enough in your post. Good luck! – madlymad Mar 14 '15 at 17:04
  • Thank you, I have edited my description. I want to hide adUnit layout and all its subviews. – guillaume Mar 14 '15 at 17:09
  • A screenshot would be great. Could you specify "I still have a blank space on my screen"? – traninho Mar 14 '15 at 17:10
  • And also where did you try to set the visibility? I mean in which class / method? – traninho Mar 14 '15 at 17:13
  • I added a screen. I'm trying to hide my layout from onBindViewHolderCursor() in a RecyclerView Adapter. – guillaume Mar 14 '15 at 17:18
  • 1
    So, if I get it right you have a RecyclerView Adapter one of the added elements is the adUnitLayout and you want to hide it? It seems to me that the problem is not the `View.GONE` that is not working properly but the fact that this is part of the RecyclerView Adapter. Have you try to remove the item from the adapter and `notifyDatasetChanged()`? – madlymad Mar 14 '15 at 17:26
  • If you set GONE on whole item in RecyclerView Adapter, it just hide the layout but not the item itself. I would rather create two types of items than hide/unhide it. Check this: http://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – traninho Mar 14 '15 at 17:28
  • In fact I have two types, one for quote and another for ads. But if there is an error while retrieving ad I want to hidde layout to not have a blank space (like on my screenshot). @madlymad what is weird is if I set visibility gone on layout I have a blank space. But If I set visibility gone on layout and all subviews (with a loop) then there is no space – guillaume Mar 14 '15 at 17:32
  • Indeed what you are describing is really weird! Actually I have never tried to hide things this way but in terms of regular ListView Adapters I know that hiding the returning rootView item results in many collapsible separators. – madlymad Mar 14 '15 at 21:25

0 Answers0