Disable StatusBar display when using AlertDialog.Builder

In the XML my MainActivity , I programmed it to use a theme with a NoActionBar and therefore the action bar does not appear. However, whenever I want to display a dialog, I call one of my DialogFragments , which uses AlertDialog.Builder to create the dialog. When I show the dialog from the main action, it shows the dialog with the ActionBar . When I close the dialog box , the action bar remains .

QUESTION How can I align and hide the action bar when the fragment disappears or does not appear in the first place?

I tried to create my own style and then wrap it in ContextThemeWrapper no avail.

getActionBar().hide() and getSupportActionBar().hide() returns null for both the main action and the fragment.

myDialog.getDialog() in MainActivity (before calling .show() ) returns null .

The code I used to create AlertDialog.Builder is inside DialogFragment onCreateDialog :

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.delete_confirmation) .setItems(fileSequence, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { File loadedFile = fileFullArray.get(which); boolean deleted = loadedFile.delete(); if (deleted) Toast.makeText(getActivity().getApplicationContext(), "File deleted!", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } } ); // Create the AlertDialog object and return it return builder.create(); 

EDIT:

Reverted to this 4 months later, and the patch added the following line in the onCreate() dialog:

this.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

The onCreate() fragment was a rewrite written in MainActivity . Greetings.

+2
source share
4 answers

The following line should be added to my onCreate() dialog

 this.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
+10
source

Last working example

The accepted answer only works for API level 16 or lower. In newer versions for AlertDialog follow these steps:

 if (Build.VERSION.SDK_INT < 16) { alert.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } else { View decorView = alert.getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); } 
+1
source

I found the resource from the official android docs. So basically for Android version 4.1 and above, use this code:

 View decorView = getWindow().getDecorView(); // Hide the status bar. int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); // Remember that you should never show the action bar if the // status bar is hidden, so hide that too if necessary. ActionBar actionBar = getActionBar(); actionBar.hide(); 

PS: Source Link to an official android document

0
source

You should use on style.xml

 <resources> <style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> </style> </resources> 

He does your activity without your ActionBar !

UPDATED:

I have a code, it should work for you!

 ab = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_DARK); ab.setNeutralButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { } }); ab.setTitle(R.string.welcome_title); ab.setMessage(R.string.message); ab.create().show(); 

UPDATED2:

here is some Java code

 requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.main); 

and some in XML

 <activity android:name=".MainActivity" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> </activity> 

Hope this will be helpful!

-1
source

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


All Articles