Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to check if the view is visible on the window?

Tags:

What's the best way to check if the view is visible on the window?

I have a CustomView which is part of my SDK and anybody can add CustomView to their layouts. My CustomView is taking some actions when it is visible to the user periodically. So if view becomes invisible to the user then it needs to stop the timer and when it becomes visible again it should restart its course.

But unfortunately there is no certain way of checking if my CustomView becomes visible or invisible to the user. There are few things that I can check and listen to:

onVisibilityChange //it is for view's visibility change, and is introduced in new API 8 version so has backward compatibility issue onWindowVisibilityChange //but my CustomView can be part of a ViewFlipper's Views so it can pose issues onDetachedFromWindows //this not as useful onWindowFocusChanged //Again my CustomView can be part of ViewFlipper's views.
So if anybody has faced this kind of issues please throw some light.
like image 885
bhups Avatar asked Sep 04 '10 21:09

bhups


People also ask

What is Android visibility?

A View's visibility status is of Integer type and can have one of three options: VISIBLE (0) - The View is visible to the user. INVISIBLE (4) - The View is invisible to the user, but still takes up space in the layout. GONE (8) - The View is invisible, and it does not take up space in the layout.


1 Answers

In my case the following code works the best to listen if the View is visible or not:

@Override protected void onWindowVisibilityChanged(int visibility) {     super.onWindowVisibilityChanged(visibility);     Log.e(TAG, "is view visible?: " + (visibility == View.VISIBLE)); } 
like image 77
goRGon Avatar answered Oct 11 '22 09:10

goRGon