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);
}
}
}
try that setVisible(0) to visible true . and setVisible(4) to visible false. the text can be invisible but the button and datepicker no.
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.
Moving the code from onLayout() to onMeasure() solves the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With