Home » Android » android – Under which circumstance should INVISIBLE used instead of GONE?

android – Under which circumstance should INVISIBLE used instead of GONE?

Posted by: admin June 15, 2020 Leave a comment

Questions:

For a View that is sometimes visible and sometimes not, depending on the user’s actions, (staying in the same activity) what is prefereable?

myView.setVisibility(View.INVISIBLE); // or
myView.setVisibility(View.GONE);

No other element depends on myView‘s size or dimensions, so the graphical result is the same.

As INVISIBLE doesn’t change the view’s dimensions, I guess it’s more preferable than GONE because with GONE we possibly have to measure and redraw other views unnecessarily. And then re-measure and re-draw them when myView is made VISIBLE again.

How to&Answers:

You’re on the right track thinking about the impact on measuring. Which one is more efficient all depends on how frequently you are changing the view’s visibility.

For example, if the view is not visible for a majority of the time, making it GONE would probably be more efficient, because the system would not be needlessly measuring and laying out your invisible view whenever it needs to adjust other views on the screen.

On the other hand, if the view changes between visible and invisible frequently, you might get better performance from INVISIBLE as you would potentially avoid an extra measure/layout on each transition.

Answer:

I want to add some experience on this from my side:

I have an animated button (using a ValueAnimator). The button moves from one side to another.

This button will switch the content of a list.

  • first approach: use one listview and change the adapter => Not smooth; the ‘button move animation’ jumps to it’s ending position, because the loading of the new adapter takes too much time from the UI thread. The button only has 200 ms to perform it’s animation and this time is completely taken by the ListView.
  • I added a second ListView, using a ViewSwitcher => same problem.
  • Because a ViewSwitcher in fact just sets the view to VISIBLE or GONE, I expected the redrawing when setting the list to VISIBLE cost to much UI thread time. So what I did now was add a FrameLayout, put the two ListViews in there and set it to INVISIBLE instead of GONE. Now the stutter is not seen anymore!

Hope this helps someone.

Answer:

I other elements are not dependent upon the visibility of your view then you can use either of them..Both would be serving the same purpose.