Search code examples
androidandroid-layoutandroid-actionbarandroid-windowmanager

Hide title bar if it's not an ActionBar - predict ActionBar presence before setContentView?


If my fullscreen preference (off by default) is on, I want my Activity to hide the title bar as well as the notification bar. I can do that with requestWindowFeature(Window.FEATURE_NO_TITLE); + FLAG_FULLSCREEN etc. or perhaps setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);

Either of these must happen before setContentView() or they will crash. The hard part is:

  • I target API levels 3 to 14, and I only want to hide the title bar if the Activity is not going to use an ActionBar. Otherwise, FEATURE_NO_TITLE will hide the ActionBar and lose some important controls. (I'll instead settle for "lights-out" mode.)
  • I can't see how to find out whether an ActionBar will be used until after setContentView(), when it's too late.

Things that don't work:

  • getActionBar() returns null at this early stage.
  • getWindow.hasFeature(Window.FEATURE_ACTION_BAR) is false.
  • Knowing (by reflection) that ActionBar exists as a class is insufficient, because, sticking with the default device theme, that might not always use ActionBar on some Ice Cream Sandwich devices, even though the platform has it. (?)
    • Alternatively: can I rely on ICS always having an ActionBar? Why?
    • Edit: ok, maybe ICS' title bar is still an ActionBar, just with no items and not even the overflow button? (In which case I still want to hide it.) Can anyone shed any light there?

Any ideas? :-)


Solution

  • I think what you want to use instead is ActionBarSherlock. It takes care of all the reflection and API level detection for you.