1

I have a full screen activity, but when I show a alert dialog above it, System UI showed (System Notification Bar). Can anybody help me ? I don't want it to be visible. Is there is any way I can hide the system notification bar from showing when the alert dialog appears?

enter image description here

enter image description here

Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
Student414
  • 56
  • 6
  • Your question is not clear, Add expected and current UI images – Charu Feb 06 '17 at 05:14
  • Show your code related dialog. – Ethan Choi Feb 06 '17 at 05:15
  • it is a very simple alert dialog made by AlertDialog.Buidler; – Student414 Feb 06 '17 at 05:28
  • AlertDialog.Builder b = new AlertDialog.Builder(context); – Student414 Feb 06 '17 at 05:29
  • i use this way to make activity fullscreen:protected void onResume() { final View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { sHandler.post(mHideRunnable); // hide the navigation bar } }); super.onResume(); – Student414 Feb 06 '17 at 05:29
  • Possible duplicate of [How to maintain the Immersive Mode in Dialogs?](http://stackoverflow.com/questions/22794049/how-to-maintain-the-immersive-mode-in-dialogs) – TheIT May 12 '17 at 02:37

4 Answers4

5

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.

3

this works for me, tested on api 21 and 16

 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    });
WenChao
  • 3,586
  • 6
  • 32
  • 46
2

Which Context-object are you using when instantiating your Dialog?

Maybe try using your activity? You can do this by passing "this" as context.

I know some people are calling getApplicationContext() which is not (always) the right way.

  • 1
    My activity has set to flags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; but always show navigation bar when i open a dialog use activity's context. – Student414 Feb 08 '17 at 07:30
  • it is work well when no dialog above my activity(to be honest, it's fragment) – Student414 Feb 08 '17 at 07:32
0

The other answers mostly use the flags for the setSystemUiVisibility() method in View. However, this API is deprecated since Android 11.

Here are code snippets for showing / hiding system bars with the new API as well as the deprecated one for backward compatibility:

fun Window.hideSystemUI() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        insetsController?.let {
            // Default behavior is that if navigation bar is hidden, the system will "steal" touches
            // and show it again upon user's touch. We just want the user to be able to show the
            // navigation bar by swipe, touches are handled by custom code -> change system bar behavior.
            // Alternative to deprecated SYSTEM_UI_FLAG_IMMERSIVE.
            it.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            // make navigation bar translucent (alternative to deprecated
            // WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            // - do this already in hideSystemUI() so that the bar
            // is translucent if user swipes it up
//                window.navigationBarColor = getColor(R.color.internal_black_semitransparent_light)
            // Finally, hide the system bars, alternative to View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            // and SYSTEM_UI_FLAG_FULLSCREEN.
            it.hide(WindowInsets.Type.systemBars())
        }
    } else {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        @Suppress("DEPRECATION")
        decorView.systemUiVisibility = (
                // Do not let system steal touches for showing the navigation bar
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        // Hide the nav bar and status bar
                        or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_FULLSCREEN
                        // Keep the app content behind the bars even if user swipes them up
                        or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        // make navbar translucent - do this already in hideSystemUI() so that the bar
        // is translucent if user swipes it up
        @Suppress("DEPRECATION")
        addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
    }
}


/**
 * Shows the system bars and returns back from fullscreen.
 * @see hideSystemUI
 * @see addSystemUIVisibilityListener
 */
fun Window.showSystemUI() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        // show app content in fullscreen, i. e. behind the bars when they are shown (alternative to
        // deprecated View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION and View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        setDecorFitsSystemWindows(false)
        // finally, show the system bars
        insetsController?.show(WindowInsets.Type.systemBars())
    } else {
        // Shows the system bars by removing all the flags
        // except for the ones that make the content appear under the system bars.
        @Suppress("DEPRECATION")
        decorView.systemUiVisibility = (
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    }
}
Dhaval Baldha
  • 241
  • 3
  • 5