ReGaSLZR
ReGaSLZR

Reputation: 413

In terms of performance, which is better: foo.setVisibility(View.GONE) or parent.removeView(foo)?

I've been searching related forums about this but not one (yet) has detailed which one of these combo methods foo.setVisibility(View.VISIBLE) + foo.setVisibility(View.GONE) and parent.addView(foo) + parent.removeView(foo) is better in terms of performance or energy consumption...

Upvotes: 0

Views: 415

Answers (5)

Darish
Darish

Reputation: 11481

foo.setVisibility(View.VISIBLE)
foo.setVisibility(View.GONE)

is preferred over

parent.addView(foo)
parent.removeView(foo)

if you want to show/hide frequently.

Upvotes: 2

Satish Gunjute
Satish Gunjute

Reputation: 11

Use View.GONE on views that don't consume a lot of memory (like a TextView) and use parent.removeView(view) on views that are a lot of memory (like a WebView)

Upvotes: 1

Arun Shankar
Arun Shankar

Reputation: 2295

Definitely VISIBLE and GONE is better compared to adding and removing views.

1) Like others have said, it definitely has better performance than actually removing and adding views

But the next point according to me is more important

2) You can still do a lot of things with the View(getText or setText) etc with the views even when you do a GONE on the view, but if it you remove and add views, the View itself does not exist, preventing you from doing anything with it

Upvotes: 0

Vyacheslav
Vyacheslav

Reputation: 27211

It depends on your implementation. But in most cases it doesnt matter.

Your custom Viewmay have got a 'huge' constructor or some methods are overloaded using time/processor consumed calculations.

Nonetheless, in case of usual view (e.g., a TextView) it really doesnt matter.

if the visibility state equals gone no methods of this view are used. While layout rendering this view is ignored.

Upvotes: 1

Abhijit Parida
Abhijit Parida

Reputation: 127

Invisible views are measured but not drawn. Gone views are not measured. So there is a slight performance difference.

Upvotes: 0

Related Questions