Prevent StatusBar from showing when using AlertDialog.Builder

前端 未结 5 812
半阙折子戏
半阙折子戏 2021-01-22 10:34

In the XML of my MainActivity, I have programmed it so that it uses a theme with NoActionBar and therefore there is no ac

相关标签:
5条回答
  • 2021-01-22 11:00

    For API Level 17 and up, see gprathour's answer.

    For API Level 16 and lower, the following line needed to be added to my Dialog's onCreate()

    this.getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
    0 讨论(0)
  • 2021-01-22 11:00

    You should use on style.xml

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

    It makes your activity without your ActionBar!

    UPDATED:

    I have some code, It should works on 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 some code on Java

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

    and some on XML

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

    Hope it be helpful!

    0 讨论(0)
  • 2021-01-22 11:04

    Calling this method I've created at the Dialog's onCreate() fully resolved the problem of status bar show or show/hide.

    private void hideStatusBar(Window window) {
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    
    0 讨论(0)
  • 2021-01-22 11:10

    I found a resource from the android official docs. So basically, for android version 4.1 and higher, 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 official android doc

    0 讨论(0)
  • 2021-01-22 11:18

    Latest Working Example

    The accepted answer works only for API Level 16 or lower. In newer versions, for AlertDialog do the following:

    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);
    }
    
    0 讨论(0)
提交回复
热议问题