Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding Status Bar while showing Alert Dialog Android

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?

like image 208
Thomas Avatar asked Nov 11 '15 04:11

Thomas


People also ask

How do I show alerts in dialog?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.


2 Answers

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);
    }
}
like image 175
Alexander Skvortsov Avatar answered Sep 21 '22 13:09

Alexander Skvortsov


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.

like image 36
Show your working Avatar answered Sep 19 '22 13:09

Show your working