17

I'm making an app where an alert dialog pops up when you hit a certain button. The status bar needs to be hidden, so I have a method in my activity:

private void hideStatusBar(){
    if (Build.VERSION.SDK_INT < 16){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
    }
}

I call this method in the activity's onCreate method, and it works fine until the alert dialog pops up. As soon as the alert dialog is shown, the status bar comes back. I tried the following:

alertDialog.show();
hideStatusBar();

which didn't work.Then I overrode the onWindowFocusChanged method for my activity:

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideStatusBar();
}

which makes the background of the status bar transparent, but still doesn't hide it. Is there any way to keep the status bar hidden when the alert dialog is shown?

Thomas
  • 1,123
  • 3
  • 13
  • 36

4 Answers4

10

Each dialog has its own Window which have its own style.

In your case hideStatusBar() doesn't work because its called from activity's onCreate() it means it tries to change appearance of activity's window, but not the dialog's window.

The solution is:

Subclass AlertDialog. Move there hideStatusBar() and call it from dialog's onCreate().

It means you have to have a deal with Dialog.getWindow() rather Activity.getWindow()

Here a little sample:

public static class TranslucentDialog extends AlertDialog {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}
Alexander Skvortsov
  • 2,696
  • 2
  • 17
  • 31
9

Build the AlertDialog using the AlertDialog.Builder and create the AlertDialog.
Prior to calling show() set the Window flags for the dialog to not focusable.
After showing the dialog set the SystemUiVisibility flags on the decorView of the Window representing the AlertDialog, and clear the not focusable flag.

    AlertDialog.Builder adBuilder = new AlertDialog.Builder(this);
    AlertDialog alertDialog = adBuilder.setCancelable(false).
            setMessage("Turn ended, Click OK").
            setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                }
            }).create();
    alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    alertDialog.show();
    alertDialog.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

Now when the AlertDialog is shown the SystemUI elements don't appear. Hope this helps.

  • The amount of flags you are passing to SetSystemUiVisitiliby() is overkill, e.g.`View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION` only has any effect if `View.SYSTEM_UI_FLAG_HIDE_NAVIGATION` is not being used. Its best to refer to the official docs at https://developer.android.com/reference/android/view/View.html to choose which flags to use for each individual's use case. Aside from that, great answer! – setholopolus Jan 22 '19 at 19:30
  • After showing my `AlertDialog` `getWindow()` always returns `null` so I can’t set any of the flags. Anyone know why this may be happening? – Sakiboy May 24 '19 at 15:23
  • 1
    I ended up using this answer for `DialogFragments`: https://stackoverflow.com/a/24549869/2371425 – Sakiboy May 24 '19 at 17:07
  • 6
    Using ```alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); alertDialog.show();``` was enough for me to show without any system ui jumping in – Monster Brain Jul 16 '19 at 06:30
2

Despite the answers here, I had a lot of trouble with this. Here is the solution I found to hide the status bar in Kotlin.

dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
dialog.show()

Note that if you keep the dialog around and want to show the same dialog again after it is dismissed, then you must set the flags again.

Trevor
  • 1,349
  • 10
  • 16
  • if this is used, the keyboard will not be displayed – Arty Morris Apr 11 '21 at 08:33
  • tried to use dialog.window?.let { WindowInsetsControllerCompat(it, it.decorView).apply { hide(WindowInsetsCompat.Type.systemBars()) } } But it behaved not normal, but this solution workd – kallis Apr 13 '23 at 03:40
-2

Try this,

final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity_no_title_dialog);
dialog.show();
Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35