❮ zur Übersicht

Android: size depending orientation lock

  • Autor:
  • Datum: 18.01.2013

We had a case in an internal app, where on Phones only the Portrait mode should be possible and on Tablets only the Landscape mode. So I googled a bit and tried out some things, and here is the solution I found for this problem.

First, in each Activiy in the AndroidManifest (or each Activity that should have this behaviour, but I prefer a consistent behaviour for the whole app), declare the following:


This will prevent the Activity to switch orientations if the user rotates the device. Then create a BaseActivity that all your Activities will be extending. In the onCreate we’ll do the other part of the trick.

    protected void onCreate(Bundle savedInstanceState) {

        if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                && (this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) < Configuration.SCREENLAYOUT_SIZE_LARGE) {
        }else if((this.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE){


Here we check for the requested screen orientation and change it according to the screen size of the device. If it’s at least a large (as in Configuration.SCREENLAYOUT_SIZE_LARGE) display, only allow Landscape, otherwise, only allow Portrait mode. We need this, because the Activity is normally started with the same orientation the device had on start of the Activity.

Now only make sure you have a Landscape layout (res/layout-land) and a Portait layout (res/layout-port) for all Activities that haven’t got a layout file that’s used for both orientations.

Also make sure to call the super.onCreate() in the child classes onCreate() first, otherwise it could load the wrong layout!

For now this small solution worked for us, but we don’t have that many different devices. If you encounter any device this doesn’t work with, or encounter some other issuese with it, please let us know!

Tags: android Landscape layout lock orientation Portrait screen size Tablet