Is there a way to lock different screen orientation for different screen sizes? So for screens with smallest width smaller than 600 dp to lock portrait orientation and for screens with smallest width greater than 600 dp to lock landscape orientation? I understand that this can be done programmatically with setRequestedOrientation
, but can it be done in xml resources somehow? In Android.manifest, or by referencing different resource values defined in res folders?
Option 1: Use the following code for each activity inside AndroidManifest.xml:
android:screenOrientation="nosensor"
The "nosensor" setting tells your app to use the natural orientation for the device, and will not change if the user rotates the device. So tablets will use landscape orientation for your app, and phones will always use portrait orientation.
Next you can create a separate layout file for landscape native devices (i.e. tablets) by creating a folder called layout-land in your res folder. Put the XML layout files for tablets into the layout-land folder.
Option 2: Use the smallest width qualifier, as described in the Android developer documentation here: http://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali
In this case you will create a layout folder in the res directory called layout-sw600dp. Inside there you will put your alternate XML files for devices with more than 600dp. In the top level of the XML files for your linear layout, or relatively layout, for example, be sure to include the following:
android:orientation="horizontal"
In your main XML files for devices with less than 600dp, you will force them to vertical orientation:
android:orientation="vertical"