Wednesday
Mar032010
Switching layouts when screen orientation changes in Android
Wednesday, March 3, 2010 at 10:28AM
When a displays orientation is changed you may find that your original layout doesn’t scale well. Every time the device orientation changes you current activity is destroyed and recreated. You can change your layout based on the current screen orientation in onCreate()
private void setContentBasedOnLayout()
{
WindowManager winMan = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
if (winMan != null)
{
int orientation = winMan.getDefaultDisplay().getOrientation();
if (orientation == 0) {
// Portrait
setContentView(R.layout.alertdialogportrait);
}
else if (orientation == 1) {
// Landscape
setContentView(R.layout.alertdialoglandscape);
}
}
}
For more information see Wei-Meng Lee’s article on Dev-X: http://www.devx.com/wireless/Article/40792/0/page/1
Rory |
Post a Comment | in
Android
Android
Reader Comments