Thursday, November 25, 2010

How to disable title bar in portrait / Landscape mode

This post talks about how to hide/show title bar in portrait and landscape.

There are scenarios where we might want to display a title bar in portrait and not in landscape due to space limitations in landscape.

How do we do this ??

- Every time a screen is rotated from landscape to portrait or vice versa, the activity is destroyed and created again.

- In onCreate(), check the screen sizes and decide it is in portrait or Landscape.

- Based on the mode, set the Window attribute Window.FEATURE_NO_TITLE using requestWindowFeature() api.

Note that requestWindowFeature() api needs to be called before setting the content view.



onCreate()
{
setTitleBar(); --> Call to decide to set whether title bar or not.
setContentView(R.layout.main);
}

public void setTitleBar()
{
Display display = ((WindowManager)mContext.getSystemService(mContext.WINDOW_SERVICE)).getDefaultDisplay();

int width = display.getWidth();
int height = display.getHeight();


if(width > height)
{
/* In Landscape */
requestWindowFeature(Window.FEATURE_NO_TITLE);
}

}

No comments:

Post a Comment