Search code examples
androidlandscapetitlebar

Hiding Android title bar only in landscape mode


I have an activity and want to have these conditions:

  • My activity has title bar in portrait mode.
  • The title bar will hide when I turn the device to go to landscape mode.
  • The activity should not to recreate when goes to landscape mode, by using "configChanges" in Manifest.

I know this question is asked before, but no correct answer I found.


Solution

  • Add in style.xml

    <style name="generalnotitle" parent="Theme.AppCompat.Light" >
        <item name="android:windowNoTitle">true</item>   <!-- //this -->
    </style>
    

    add in manifiest file Activity ,

      android:theme="@style/generalnotitle">
    

    To get Orientation of Device

     int display_mode = getResources().getConfiguration().orientation;
    
    
    if (display_mode == Configuration.ORIENTATION_LANDSCAPE) {
    
            getSupportActionBar().hide(); //<< this
    
        } else {
    
    
    
        }
    
    setContentView(R.layout.activity_main);
    

    Portrait Mode with title bar

    Landscape mode without Title bar

    Lanscape mode without title bar