Reputation: 508
If foo is a view, what is the difference between foo.setVisibility(View.GONE)
and fooParent.removeView(foo)
? I am particularly interested in memory consumption of the view, before and after both statements.
Does a view with visibility set to GONE consume memory ?
Upvotes: 12
Views: 4737
Reputation: 492
I know this question is too old, but after what I have encountered in my recent projects, I want to give an idea of how View.GONE
works, and how it can come in handy for some runtime view position manipulations.
Suppose you have added two child views inside a RelativeLayout
, cv1
and cv2
, and that cv2 is relatively below cv1(using layout_below
). Then if you plan to set visibility of cv1 to GONE
in runtime, then it won't cause any runtime error.
That's amazing to see because cv2 was relative to cv1, and now cv1's visibility is GONE
.
So what happens actually with visibility GONE
is that,
view's height and width becomes zero, but, view stays in the parent, like a tiny dot or point.
So, even if any view is placed relative to it, it will be like, that view will occupy its place(here cv2 will occupy place of cv1 in runtime). Later on, if cv2's visibility is turned back to VISIBLE
, than, cv1 will now occupy its actual position, with cv2 below it.
This link describes my usecase.
This approach of showing and hiding view comes in handy, when you don't want to handle the hassle of manipulating the LayoutParams
of a view in runtime, when your usecase is this simple.
On the other hand, parent.addView()
/ parent.removeView()
approach is opted when your view hierarchy is complex, and you want to manipulate margins, paddings, and relative positions of the views drastically during runtime.
Upvotes: 0
Reputation: 6158
suppose,
if you need to delete all the available option of flying once you select a particular flight. then go with fooParent.removeView(foo).
or,
if you need to selection of a particular flight all the flying options are disappeared, and deselection of flying option again show all the available options then go with foo.setVisibility(View.GONE) and foo.setVisibility(View.VISIBLE)
setVisibility(View.VISIBLE) = setVisibility(0)
setVisibility(View.GONE) = setVisibility(8)
setVisibility(View.INVISIBLE ) = setVisibility(4)
Upvotes: 0
Reputation: 56925
If you need to remove them and then show them again, it could be better to just set visibility to gone and then change back to visible again.
If you on the other hand don't need them visible again, simply remove them.
Upvotes: 9