-1

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.

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
ShlomoA
  • 31
  • 1
  • 4

3 Answers3

3

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

Herry
  • 7,037
  • 7
  • 50
  • 80
  • Thank you very very much. This is something that I have been struggling with a lot of hours. You are the only one that understood the problem. – ShlomoA Oct 25 '17 at 09:33
  • After working with it for some time, I found a major bug in it. The problem was that when I show a "hidden" edit-box, It didn't push the dialog up, so I couldn't click on the buttons. Eventually, I removed the "clearFlags" line, and added it only when I show the edit control. Also, when the app shows the edit-control and do the "clearFlags", it shows the keyboard, but also the navigation bar. So in the end I will need to change the Tablet configuration from"adb" with the command: pm disable com.android.systemui – ShlomoA Oct 26 '17 at 12:20
0

Try to see there are the official doc

0

use these methods to show/hide systemUI

  public void hideUI() {
     decorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
        | View.SYSTEM_UI_FLAG_IMMERSIVE);
  }


  public void showUI() {
      decorView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  }