3

I want my Android application to behave like below. 1) Portrait mode: With title bar 2) Landscape mode: Without title bar (because of height limitation)

I know I can realize 1) using requestWindowFeature(Window.FEATURE_NO_TITLE), but how can I dynamically change from 1) to 2) when I rotate my phone?

tokentoken
  • 682
  • 7
  • 15

2 Answers2

5

When the phone is rotated, your activity is shut down and recreated. Inside onCreate, you can grab an instance of Display (using getWindowManager().getDefaultDisplay()) and query its width, height, and/or rotation to decide if you want a title feature, all before setting the content view.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
4

This answer suggests you can't change the feature during the lifecycle of the activity (as happens when the orientation changes), so they recommend implementing your own title (jump to the "::Edit::" part):

Hiding Title in a Fullscreen mode?

::Edit::

Well if you are trying to dynamically show and hide it during the lifetime of the activity I am not sure if you can do that with the official Window Title due to the note that has been mentioned about window features needing to be set before setContentView() is called.

One thing that you could do is implement your own title bar and dynamically show and hide that... I put together this example that should set you o nthe right track

Since when you change orientation, your app goes through a set of lifecycle changes, you have an opportunity in onCreate to show or hide your title.

http://stuffthathappens.com/blog/2008/11/26/android-lifecycle-triggers-part-2/

Diagram of lifecycle when orientation changes

Or you specify a different layout altogether for landscape mode:

http://developer.android.com/resources/tutorials/hello-world.html

Landscape layout

When you want a different design for landscape, put your layout XML file inside /res/layout-land. Android will automatically look here when the layout changes. Without this special landscape layout defined, Android will stretch the default layout.

Community
  • 1
  • 1
Bert F
  • 85,407
  • 12
  • 106
  • 123
  • 1
    Thank you, I thought I cannot change the feature during application lifecycle, and I forgot that onCreate is called every time I rotate my phone. – tokentoken Feb 01 '11 at 00:47