Hide the title / notification bar when the device is landscape oriented

I want my title bar to be hidden when I turn my device into landscape. I saw an XML parameter to hide the title bar, and the following execution example programmatically:

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

BUT, I use the parameter configChangesfor orientationand screenSize, therefore, my activity is not recreated again when it focuses on the landscape (I do this for some reason). Therefore, I cannot use the above methods, since these calls must be completed before setContentView().

So any ideas? Thank.

+5
source share
4 answers

I searched for the answer, ended up here, put the pieces together, and here they are:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
  getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
+12

, configChanges Activity , onConfigurationChanged:

@Override
public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);
     if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          getActionBar().hide();
     } else {
          getActionBar().show();
     }
}

getSupportActionBar() getActionBar().

+6

,

, .., " " :

getResources().getConfiguration().orientation

, .

+2

"AndroidManifest.xml" "NoTitleBar" theme, :

<activity
    android:name="com.my_package.MyActivity" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="navigation|orientation|screenLayout|layoutDirection">
-1

All Articles