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
- When the app is launched on a smartphone the app should be displayed in portrait mode.
- If the user turns his smartphone in any direction the app should always remain in portrait mode.
- If a second activity is startet on the app, this second activity should run in portrait mode.
Working with a tablet
- 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.
- When the device will be rotated, the app should switch between portrait / landscape mode.
- A second activity should always be startet in the orientation of its previous activity.
- If the device is rotated while the second activity is visible this activity should switch between portrait / landscape mode.
- 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.