Sunday, January 31, 2016

[Performance/Memory Consumption] setVisibility(View.GONE) vs setVisibility(View.INVISIBLE) vs parent.removeView()

There are three ways to hide views. Let's find out which one is the most efficient in performance.

Is there performance difference between setVisibility(View.GONE), setVisibility(View.INVISIBLE), and parent.removeView()? If so, which one is the fastest?

Short Answer: It depends.


View.GONE vs View.INVISIBLE

  • View.GONE This view is invisible, and it doesn't take any space for layout purposes.
  • View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.

In general, making unused views to View.GONE is more efficient, because you are skipping measuring the view size step.
However, if the view is frequently changing its visibility from View.GONE to View.VISIBLE, then it requires re-measuring the view size and the layout location of other views in-relation to the view. When you change from View.INVISIBLE to View.VISIBLE, you are skipping this measuring process.

View.GONE vs parent.removeView()

View in View.GONE will still consume memory whereas parent.removeView() will release its view memory. So, if the view is a large memory consuming view (eg. webview) it is a good idea to remove it from its parent view.
However, if you know you're going to show the view again, then it is better to use View.GONE.

No comments:

Post a Comment