Search code examples
androidandroid-layoutstatusbar

Is there a way to check the visibility of the status bar?


I have a service that should check periodically visibility of status bar, when some top activity is (or not) in fullscreen mode. Is it possible?


Solution

  • Finally I have discovered how to check if statusbar is visible or not. Its some kind of hack, but it works for me. I created that method in my Service:

    private void createHelperWnd() {
            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            final WindowManager.LayoutParams p = new WindowManager.LayoutParams();
            p.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
            p.gravity = Gravity.RIGHT | Gravity.TOP;
            p.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            p.width = 1;
            p.height = LayoutParams.MATCH_PARENT;
            p.format = PixelFormat.TRANSPARENT;
            helperWnd = new View(this); //View helperWnd;
    
            wm.addView(helperWnd, p);
            final ViewTreeObserver vto = helperWnd.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
                @Override
                public void onGlobalLayout() {
    
                    if (heightS == helperWnd.getHeight()) {
                        isFullScreen = true;
                    } else {
                        isFullScreen = false;
                    }
                }
            });
    
        }
    

    where widthS and heightS our global screen size; Here I just compared invisible helper window height to screen height and make decision if status bar is visible. And do not forget to remove helperWnd in onDestroy of your Service.