Search code examples
androidandroid-manifesttabletscreen-orientation

Force android screen Orientation depending on screen size


I am writing an application that allows users to select various things on the screen. On large 10 inch screens this looks best in landscape so I force landscape in the manifest file.

However, I want to make the application friendly for users of 8 (or 7) inch tablets as well. On these screen sizes the application should be forced in portrait mode.

Does anyone know how I can force the screen orientation depending on the screen size?


Solution

  • To find out if the device is a 7, 8 or 10 inches tablet, you can check this.

    One way you can manipulate the device orientation is in every activity, after onCreate(), like this:

        // set the orientation of the device
        if(isTabletWith10Inches) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }