Fix screen orientation to portrait on small Android devices but allow rotating the screens for tablets

Posted by admin on Februar 9, 2018


On small Android devices like phones it is sometimes better that an app is fixed to portrait mode. But if the application also can be used on devices with bigger screens, this limitation should not be active.

The following images displays the desired behaviour.

Working with a smartphone


  1. When the app is launched on a smartphone the app should be displayed in portrait mode.
  2. If the user turns his smartphone in any direction the app should always remain in portrait mode.
  3. If a second activity is startet on the app, this second activity should run in portrait mode.

Working with a tablet


  1. The app launched on a device with w big screen, like a tablet, should open in the depending on the actual orientation of the device. No change should be performed.
  2. When the device will be rotated, the app should switch between portrait / landscape mode.
  3. A second activity should always be startet in the orientation of its previous activity.
  4. If the device is rotated while the second activity is visible this activity should switch between portrait / landscape mode.
  5. If the user navigates to the first activity, this activity should be displayed in the same orientation of the second activity.
    In the example the activity 1 should automatically be displayed in portrait mode, even it was last seen be the user in landscape mode.

So the question is, how can this behaviour be achieved in android.


Change orientation of the app if the app runs on a small screen

Define a fix orientation for all activities in an app is easy but does not allow devices with big screens to change the orientation at all. So how can an app determine on runtime if the current screen it big enough to show the ap in landscape mode.

There are different options to find out the size of the screen. A very elegant way to define options and values for an app depending of constraints is to work with different value files, which can be qualified with constraints.

Lets first create a new resource file (or reuse an existing one) and define the default value if the screen should be fix shown in portrait mode.



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <bool name="portrait_only">true</bool>
  4. </resources>

In our resource file a boolean value is defined with the name "portrait_only" and the value of "true".

Now create a second resource file with a constraint to the size of the screen.


In this Example we create a new app_defaults.xml file with the qualifier Screen with is bigger then 900dp.

In this file wie set the value of ou boolean to "false".


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <bool name="portrait_only">false</bool>
  4. </resources>

So if android is displaying an activity on screen bigger then 900dp, the value of the boolean variable is set to false.

In the onCreate method of the activities the screen orientation can now be fixed based on this variable.

Activity1 and Activity2

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  5. if(getResources().getBoolean(R.bool.portrait_only)){
  6. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  7. }
  8. ...
  9. }

At line 5 we define the check against the variable defined in the resource files.

In line 6 we set the screen orientation fix to portrait if the variable is true.

Issue with only this changes

If we made only this changes, we are facing the problem, that after the first activity on a device with small screen in landscape orientation is forced to run in portrait mode, the second activity also tries first to launch the activity in lanscape mode and is afterwars forced to run in portrait mode.


Always run a second activity in the orientation of the previous activity

In the AndroidManifest.xml the orientation of the screen can be defined. With the value "behind", android opens the activity in the orientation of the previous activity.

  1. <activity android:name=".activity.Activity2"
  2. android:screenOrientation="behind"/>

The app now works fine on smartphones, but the special case described in the tablet problem in step 4 and 5 is, that the activity one which was last open in landscape mode, will be displayed in landscape mode again if activity 2 will be closed. To enforce Activity 1 to switch from landscape to portrait mode, after activity 2 is closed, the following code in the onResume method of activity 1 is needed.

onResume method of Activity1 and Activity2

  1. @Override
  2. public void onResume() {
  3. super.onResume();
  4. setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);
  5. }

If there are more then two activities the procedure shown here is the same. Set the screenOrientation value in the AndroidManifest.xml and override the onResume method.


With this changes the app changes and fixes the screen orientation to portrait mode if the app is running on a device with small screen. If the app runs on a device with a big screen, the app switches between portrait and landscape mode when turning the device.

Als when navigating back to a previous activity after changing the orientation of the screen, the previous activity will show up in the same orientation as the previous activity.

Source Code

In this repository you will find a demo application implementing the described solution.

Your Comments

Leave a Reply

(Your email will not be publicly displayed.)

Captcha Code

Click the image to see another captcha.