Search code examples
androiduinavigationbarnavigationbar

How to hide the system navigation bar in android


I have followed the Android developers guide on: how to hide the system navigation bar.

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

It works OK, until I show an AlertDialog. When the Dialog is displayed, the navigation bar (the three icons: square, triangle and circle) are displayed on top of the app's controls.


Solution

  • NOTE : System Navigation will still appear when ever you show any AlertDialog but when you dismiss it , It will hide again. If you still not want this behavior then use Center View to create Alert like view.

    You can try following approach which i have used .

    /**
         * Hide system NavigationBar and StatusBar
         */
        @TargetApi(Build.VERSION_CODES.KITKAT)
        public void hideNavigationBar()
        {
            final View decorView = getWindow().getDecorView();
            decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visibility) {
                    Log.i("LOG","Menu Shown is this"+ visibility);
                    decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_IMMERSIVE);
    
                }
            });
        }
    

    Call above method from onResume of Activity and make sure you override following method in activity.

    @Override
        @TargetApi(Build.VERSION_CODES.KITKAT)
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            if (hasFocus)
            {
                getWindow().getDecorView().setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_IMMERSIVE
                                );
            }
        }
    

    You should use following approach to hide navigation when Alert created.

    public static void showAlert(Context context,String title,String message) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
            alertDialogBuilder.setTitle(title);
            alertDialogBuilder.setMessage(message);
            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
           final AlertDialog alertDialog = alertDialogBuilder.create();
            //Here's the magic..
    //Set the dialog to not focusable (makes navigation ignore us adding the window)
            alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    
            alertDialog.show();
            //Set the dialog to immersive
            alertDialog.getWindow().getDecorView().setSystemUiVisibility(
                    ((Activity)context).getWindow().getDecorView().getSystemUiVisibility());
    
    //Clear the not focusable flag from the window
           alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        }
    

    I found above working code from this SO