7

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);
        }
    }
}
0

3 Answers 3

1

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

0

Ok. I don't understand how it draws anything in your situation at all. But I think you should call super.onLayout() after your check. Like this:

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

LinearLayout decides how to layout it's children (vertically, or horizontally) in onLayout(), which you didn't call. More to it, after choosing, it actually starts calculating how to layout each child, and if the child's visibility is GONE, it skips it (means the child won't take any space). I think in your case, no children at all would be layed out and shown, but I might be wrong somewhere.

1
  • Whoops. That was a copy-paste error, I do, in fact call super.onLayout().
    – Adam
    Feb 4, 2011 at 15:57
0

I suspect you are making your life unnecessarily difficult. Make the extensible part of your view (the non-title bit with the few TextViews or TableLayouts) belong to a single container View - a FrameLayout seems a likely choice - and just call setVisibility() on that.

No need for looping, and you definitely don't want to do it from onLayout()... just do it from your title's onClick().

1
  • That is one way. It would be a bit cleaner, in fact.. is there any way to enforce that the content is only one view (ie. only one child view in the XML layout file)?
    – Adam
    Feb 4, 2011 at 16:27

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.