Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View with visibility state GONE taking up space on screen

I am experiencing a problem where views with a visibility state of GONE are (undesirably) taking up space on the screen. This problem occurs always on API level <= 7 devices, but only recently on 8+ devices (after I utilized AsyncTasks to populate some fields, as per Show a progress bar when an Activity is loading)

A bit of context: I created a custom view extending LinearLayout that contains a "title" button and (user defined; in some cases, it's a few TextViews, in others it's TableLayouts) "contents". The purpose of this view is to toggle view of the contents onClick of the title button (I don't believe there is a built-in widget for this.. I may be wrong).

In onLayout() I explicitly set the visibility state of all child views except the title to GONE, the first time it is to be drawn:

protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if(initialDraw) {
        setContentsVisible(false);
        initialDraw = false;
    }
    super.onLayout(changed, l, t, r, b);
}

public void setContentsVisible(boolean visible) {
    for(int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);

        if(child != mTitle) {
            child.setVisibility(visible ? VISIBLE : GONE);
        }
    }
}
like image 774
Adam Avatar asked Feb 04 '11 14:02

Adam


People also ask

How do I make my Android visibility disappear?

try that setVisible(0) to visible true . and setVisible(4) to visible false. the text can be invisible but the button and datepicker no.

What is visibility gone in Android?

When a View is gone, it means it doesn't take any space in the layout. When it is invisible, it will take the necessary room in a layout but you just don't see it.


1 Answers

Moving the code from onLayout() to onMeasure() solves the problem.

like image 143
Adam Avatar answered Nov 15 '22 02:11

Adam