Search code examples
androidshow-hideandroid-statusbar

How to hide status and navigation bars after first touch event?


I have been using the flags SYSTEM_UI_FLAG_FULLSCREEN and SYSTEM_UI_FLAG_HIDE_NAVIGATION to hide and show both the status and navigation bars. They are working correctly. I am hiding them as the activity starts and I want to show them again on touch event. Android automatically shows them on first touch event (this first touch event is not passed to my app). I'm thinking to use sendMessageDelayed() to hide both the bars after a certain time. How could I use this first touch event?


Solution

  • I got it by using View.OnSystemUiVisibilityChangeListener. I have just used a Handler.sendMessageDelayed inside the if condition when both status and navigation bars are visible. See the below implementation for clarity.

    Source : Responding to UI Visibility Changes

    View decorView = getActivity().getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {@Override
        public void onSystemUiVisibilityChange(int visibility) {
            if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                // TODO: The system bars are visible. Make any desired
                Message msg = mHandler.obtainMessage(HIDE_STATUSBAR); //Implement your hide functionality accordingly                
                mHandler.sendMessageDelayed(msg, 3000);
            } else {
                // TODO: The system bars are NOT visible. Make any desired
    
                }
            }
        }
    });
    

    The disadvantage with this approach is the first touch event could not be used by your app. I think you have to use other approaches like WindowManager.LayoutParams or Immersive Fullscreen Mode in that case.