Home > Tricks & Tips > Detect screen type and apply the correct orientation to application

Detect screen type and apply the correct orientation to application


Long ago, I had to detect screen type in order to apply correct orientation for my application.

In some devices, the width is smaller than the height, however, some others having the width is greater. As you can see this property clearly in the differences between smartphone and tablets.

This is my code.

public void onCreate(Bundle savedState) {
    //...

    if(isScreenLarge()) {
        // width > height, better to use Landscape
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

public boolean isScreenLarge() {
    final int screenSize = getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK;
    return screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
            || screenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE;
}

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: