73

I've got a view that is effectively is a button. Here is its XML layout (add_new.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <Button
        android:id="@+id/buttonNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bText" 
        android:onClick="addNew"/>
</LinearLayout>

When I set its visibility to GONE like this

v = getActivity().getLayoutInflater().inflate(R.layout.add_new, null);
v.setVisibility(View.GONE);

it disappears but still occupies space. Like this: enter image description here

This button is a header in the ListView, which is defined by this xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/porno" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_launcher">
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20dp" >
    </TextView>

</LinearLayout> 

And I dont want it to occupy an additional list item when its visibility is set to GONE. As it is stated in the documentation.

GONE - This view is invisible, and it doesn't take any space for layout purposes.

Any ideas on how to make it NOT occupy space?

Thanks, Dennis xx

P.S. My listview is inside of a FoldersFragment ListFragmentand here is the xml of my MainActivity where FoldersFragment is presented

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/foldersFragment"
        android:layout_width="200dip"
        android:layout_height="match_parent"
        class="com.example.fragments.FoldersFragment" >
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.fragments.DetailFragment" >
    </fragment>

</LinearLayout>
9
  • Why are you inflating view, just take Button. Commented Sep 6, 2012 at 14:22
  • Because I then set this view as a ListView header
    – Denys
    Commented Sep 6, 2012 at 14:23
  • please post your xml where u added the add_new.xml this is limited info! Commented Sep 6, 2012 at 14:24
  • Don't add this as Header of your List. Just take it Above the list in the XML where you have taken the list. Commented Sep 6, 2012 at 14:25
  • I want to see the XML in which you have taken the list. Commented Sep 6, 2012 at 14:30

16 Answers 16

54

This is an Android bug in my opinion, we just fix this issue doing this:

<FrameLayout android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/layout_to_hide"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
         //Put here your views
    </LinearLayout>
</FrameLayout>

Just hide LinearLayout with id LAYOUT_TO_HIDE with Visible.GONE and then root FrameLayout will collapse its height giving you a "hidden" with non-blank-space header.

5
  • 5
    Hi there. Do you still have not found a better solution? Is this a bug really? I'm having the same problem
    – Online98
    Commented Apr 6, 2015 at 15:59
  • 4
    how is this still a problem in 2021
    – DIRTY DAVE
    Commented Nov 5, 2021 at 9:57
  • 6
    I am also facing this issue in 2021.. any update on this?
    – Karan_Rana
    Commented Nov 20, 2021 at 12:03
  • 1
    still facing this in 2023 :))))
    – nAkhmedov
    Commented Feb 24, 2023 at 13:24
  • Yes still facing issue. Have you find any solution
    – Developer
    Commented Mar 17, 2023 at 9:10
11

set layout_width and layout_height to 0 where you want to hide item, by doing this

//if item's parent layout is LinearLayout, change according to your item xml
holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0,0));
9

All replies in this thread are suggesting a new wrapper view, which comes at a cost. The correct way of hiding a view completely is to set margins to 0 while setting visibility to GONE. In this code sample, cardView is the view I am trying to hide. The direct parent of cardView is RecyclerView, that's why we are using RecyclerView.LayoutParams - remember to replace with the right layout params.

if (cardView.getVisibility() != GONE) {
    cardView.setVisibility(GONE);
    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) cardView.getLayoutParams();
    layoutParams.setMargins(0, 0, 0, 0);
    cardView.setLayoutParams(layoutParams);
}
7
  • This is irrelevant for ListView.
    – Miha_x64
    Commented Nov 9, 2018 at 16:45
  • @Miha_x64 Can you please explain why? ListView should behave the same as any other view.
    – dev
    Commented Nov 12, 2018 at 6:52
  • The author of the question already tried GONE and then created this question. Another answer says that GONE doesn't work correctly inside ListView: stackoverflow.com/a/20786397/3050249
    – Miha_x64
    Commented Nov 12, 2018 at 9:40
  • My answer clearly states the additional step required since GONE is not enough. Your comment ignores the main part of my answer and the drawback associated with your linked answer.
    – dev
    Commented Nov 12, 2018 at 11:20
  • Margins are not specified explicitly in the question. This means that they are (0, 0, 0, 0).
    – Miha_x64
    Commented Nov 12, 2018 at 15:49
4

If this is stil needed, there is a great way to deal with it:

parentView.removeView(childView);

here an example:

final WhatEverLayout parentView, childView;
parentView = (WhatEverLayout)findViewById(R.id.parentView_xml);
childView =(WhatEverLayout)findViewById(R.id.childView_xml);
parentView.removeView(childView);
2
2

What you can do is set an if statement, whenever the condition to set the visibility to "GONE", set the view as wrap content and your space will be free, I did it and it worked for a seekbar

2
  • Please provide some example.
    – Szymon
    Commented Oct 12, 2013 at 22:35
  • Let take my example for instance. I had to show a seek bar if the item was an audio file, if not, then no seekbar. So I set the seekbar in the xml as wrap content (height and width) meaning the layout will occupy the space of the showed element. And when the seekbar is "GONE", the space is not used since the view wrap a content that is not there. If I could I would have showed a screen capture but the trick is pretty simple.
    – Jayo2k
    Commented Oct 13, 2013 at 15:46
2

This is my solution. Create a new layout file with name "special.xml", copy the code to the file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
    android:visibility="gone">

</LinearLayout>

Use condition to inflate the empty layout inside the newview. Don't Use the view.setVisibility(View.GONE);.

if(true){
  view=mInflater.inflate ( R.layout.special,parent, false );
}else{
  view=mInflater.inflate(R.layout.normal,parent,false);
  // The view that you want to be visible
}
2

header_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/mHeaderView"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/mNoHelpLayout"
                android:layout_width="match_parent"
                android:layout_height="176dip"
                android:background="#ffffffff"
                android:gravity="center"
             />
       </LinearLayout>
   </LinearLayout>

java code:

LayoutInflater mInflater =(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View outerView =  mInflater.inflate(R.layout.header_layout, null);
mHeaderView = (LinearLayout) outerView.findViewById(R.id.mHeaderView);

use mHeaderView.setVisiable() to control visiable not the outerView.setVisiable(). it works for me.

2
If you want to show itemView 

if (ItemBean.isShow()) 
{
   holder.itemView.setVisibility(View.VISIBLE);
   holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
 } else
 {
     holder.itemView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
     holder.itemView.setVisibility(View.GONE);
 }
1

You can use : viewToHide?.postDelayed({ viewToHide?.visibility = View.GONE }, durationInMillis)

1
  • worked for me, tks Commented Jun 7, 2022 at 6:15
1

For me, it worked if I did the showing/hiding on the next run loop.

parentView.post { 
   childViewToHide.visibility = View.GONE
}
1

I had similar issue when using TransitionManager.beginDelayedTransition(), in the Layout Inspector tool I see that the view is Gone, but it still taking space, I think it is bug in Android, a workaround is to execute the visibility block with View.post(Runnable action)

1

In my case I was using an animation and in my anim, I had the flag

android:fillAfter="true"

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <translate
        android:duration="300"
        android:fromYDelta="100%"
        android:toYDelta="0%" />

    
    <alpha
        android:duration="150"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

This caused the view to be set as invisible even when it was being set to GONE.

After removing android:fillAfter="true" I had no more issues and the view was then properly being set to GONE.

1

Simplest way is to force the parent view to remeasure its own height after children visibility changed.
Try something like this :

parentView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));

in this case, parent view is a LinearLayout.

0

A generic and easy solution is to avoid all thuse heck of code:

You need to add a Container Layout or a Parent Layout over your set of views that you want to hide. It can be anything like CardView or FrameLayout or etc.

Just it has to be a parent for that view that you want to hide. And you wont get all those white spaces. Coz it will consider the entire container to hide instead of considering individual views to HIDE thus, avoiding the white spaces.

0

Setting view padding and margins to 0 and then set view visibility to gone solved my problem.

-5

I don't think this is a bug.. Just use GONE to make the empty space disappear. CSS does the same thing. Try display: block vs visibility: hidden.

3
  • 1
    OP actually wants the empty space to disappear, but GONE doesn't seem to work.
    – Ealhad
    Commented Jan 9, 2016 at 13:41
  • 1
    display is not an android attribute
    – Roberto
    Commented Aug 8, 2016 at 23:21
  • Well... I was just using "display" as an example here and v.setVisibility(View.GONE) worked in my case - the same did disappear. Commented Jan 5, 2021 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.