Hide status bar when displaying Alert Dialog Android

I am creating an application in which a warning appears when you press a specific button. The status bar should 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 onCreate method and it works fine until a warning dialog appears. As soon as the warning dialog appears, a status bar will appear. I tried the following:

 alertDialog.show(); hideStatusBar(); 

which did not work. Then I redefined 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 does not hide it. Is there a way to keep the status bar hidden when a warning dialog is displayed?

+5
source share
2 answers

Each dialogue has its own window, which has its own style.

In your case, hideStatusBar() does not work, because its called from the onCreate() action means that it is trying to change the appearance of the activity window, but not the dialog box.

Decision:

Subclass of AlertDialog. Move hideStatusBar() and call it from the onCreate() dialog.

This means that you need to deal with Dialog.getWindow() rather Activity.getWindow()

Here is a small example:

 public static class TranslucentDialog extends AlertDialog { ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } 
+1
source

Try it,

 final Dialog dialog = new Dialog(context); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.activity_no_title_dialog); dialog.show(); 
-1
source

Source: https://habr.com/ru/post/1235660/


All Articles