8

How can I detect the presence of the navigation bar and hide it?

In my onCreate() I call hideNavigationBar() method to hide the navigation bar, then I register a listener to hide the navigation bar every time it becomes visible when the user touches anywhere on the screen as reported by the documentations. When the navigation bar becomes visible after a touch event the hideNavigationBar() method is called again by the listener, but it has not effect, the bar is still visible.

This is my onCreated() method:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hideNavigationBar();

        View decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            Toast.makeText(getApplicationContext(), "Visible", Toast.LENGTH_SHORT).show();
                            hideNavigationBar();
                        } else {
                            Toast.makeText(getApplicationContext(), "Not visible", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

and this is my hideNavigationBar() method:

 private void hideNavigationBar() {

        Toast.makeText(getApplicationContext(), "hideNavigationBar()", Toast.LENGTH_SHORT).show();

        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }

How can I hide the navigation bar every time it becomes visible?

Thanks

amukhachov
  • 5,822
  • 1
  • 41
  • 60
Stephan
  • 291
  • 1
  • 2
  • 9

3 Answers3

12

you could add this code to your activity's onCreate() method:

      View decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener
            (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {                
                        // TODO: The navigation bar is visible. Make any desired
                        // adjustments to your UI, such as showing the action bar or
                        // other navigational controls.
                        hideNavigationBar() 

                    } else {
                        // TODO: The navigation bar is NOT visible. Make any desired
                        // adjustments to your UI, such as hiding the action bar or
                        // other navigational controls.
                    }
                }
            });

It's generally good practice to keep your UI in sync with changes in system bar visibility. For example, you could use this listener to hide and show the action bar in concert with the status bar hiding and showing.Android-Responding to UI Visibility Changes

joe
  • 619
  • 6
  • 10
  • This works great for me. Let's me detect changes to the visibility of the navigation bar. Unfortunately it doesn't report the initial state of the bar. I've got a work around where I set a new systemUiVisibility state and then immediately revert it to make this listener fire. Seems to be doing the job for now! – TreeTrum Aug 09 '18 at 23:16
  • I am getting true value for condition every time even navigation bar is hide or visible. I am using Android Navigation gesture functionality to hide and show navigation bar.. any solutions? – Nik Mar 12 '19 at 09:52
  • @Nik I saw your comments on posts regarding the height of the navigation bar. Did you find any solution? I am not able to get the nav bar size correctly. – Aman Verma Jun 07 '19 at 12:22
  • @Sniper No, I am unable to find solution. – Nik Jun 10 '19 at 07:04
1

Do this:

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

if(!hasMenuKey && !hasBackKey) {
    // Do whatever you need to do, this device has a navigation bar
}

Original answer Check for navigation bar

Community
  • 1
  • 1
Vinicius DSL
  • 1,839
  • 1
  • 18
  • 26
0

Try calling hideNavigationBar() in a Runnable using View.post(Runnable). For example:

...
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
    decorView.post(new Runnable() {
        @Override
        public void run() {
            hideNavigationBar();
        }
    });
}
...
Soumya
  • 13,677
  • 6
  • 34
  • 49