Hiding title bar / notification bar when device is oriented to landscape
Asked Answered
A

5

11

I want my title bar to be hidden when I turn my device to landscape. I have seen the XML option to hide the title bar, and the following example of doing it 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 am using configChanges parameter for orientation and screenSize, so my activity is not re-created again when it is orienting to landscape (I'm doing this for some reasons). So I cannot use the above methods as these calls need to be made before setContentView().

So any ideas? Thank you.

Antilebanon answered 8/8, 2012 at 2:33 Comment(3)
Youtube Application is doing it somehow ... and I am sure they are not creating the activity again ( try running the video in portrait. and then turn to landscape ).Antilebanon
I don't think you can change the decorview after you have called the setcontentview. I would suggest that you start the view in a fullscreen mode and make your titlebar part of your contentview. that way on oreientation change you can hide it.Meader
read this post #4854005, suggestion define your own title, and you can hide and show as you like.Fruitarian
H
19

was searching for an 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);
}
Hippodrome answered 7/5, 2015 at 0:44 Comment(0)
L
7

Assuming you have defined configChangesfor the Activity in the manifest then you can achieve your issue overriding onConfigurationChanged method:

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

You will have to use getSupportActionBar() instead of getActionBar() if using the support library.

Lederman answered 9/10, 2013 at 14:36 Comment(1)
In AppCompatActivity it's not fired!Marquismarquisate
S
3

first check your device orientation by using following code

The current configuration, as used to determine which resources to retrieve etc, as available from the Resources' Configuration object as:

getResources().getConfiguration().orientation

Then do the necessary coding related to hide title bar and notification bar.

Sylvester answered 8/8, 2012 at 4:31 Comment(0)
I
0
@Override
public void onConfigurationChanged(Configuration config) {
     super.onConfigurationChanged(config);
     if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
          getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
          getActionBar().hide();
     } else {
          getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
          getActionBar().show();
     }
}

You can use this I have tried by my self.

Impassion answered 6/3, 2021 at 4:33 Comment(0)
P
-2

In your "AndroidManifest.xml", in the activity tag you can specify "NoTitleBar" in the theme property so that it will always hide the title:

<activity
    android:name="com.my_package.MyActivity" 
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="navigation|orientation|screenLayout|layoutDirection">
Proposition answered 22/9, 2014 at 21:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.