« Visual Studio 2010: Code Generation | Main | Moving a SharePoint site collection to another managed path »
Wednesday
Mar032010

Switching layouts when screen orientation changes in Android

 

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

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>