How to Lock Android App's Orientation to Portrait in Phones and Landscape in Tablets?

AndroidUser Interface

Android Problem Overview


I am developing an Android app whose orientation I don't want changed to landscape mode when the user rotates the device. Also, I want the locked orientation to be portrait mode on phones and landscape mode on tablets. Can this be achieved, if yes how? Thanks.

Android Solutions


Solution 1 - Android

You just have to define the property below inside the activity element in your AndroidManifest.xml file. It will restrict your orientation to portrait.

>android:screenOrientation="portrait"

Example:

        <activity
            android:name="com.example.demo_spinner.MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
        </activity>

if you want this to apply to the whole app define the property below inside the application tag like so:

        <application>
         android:screenOrientation="sensorPortrait"
        </application>

Additionaly, as per Eduard Luca's comment below, you can also use screenOrientation="sensorPortrait" if you want to enable rotation by 180 degrees.

Solution 2 - Android

You have to add the android:screenOrientation="portrait" directive in your AndroidManifest.xml. This is to be done in your <activity> tag.

In addition, the Android Developers guide states that :

> [...] you should also explicitly declare that your application requires > either portrait or landscape orientation with the > element. For example, <uses-feature android:name="android.hardware.screen.portrait" />.

Solution 3 - Android

I can see you have accepted an answer which doesn't solve your problem entirely:

android:screenOrientation="portrait" 

This will force your app to be portrait on both phones and tablets.

You can have the app forced in the device's "preferred" orientation by using

android:screenOrientation="nosensor"

This will lead to forcing your app to portrait on most phones phones and landscape on tablets. There are many phones with keypads which were designed for landscape mode. Forcing your app to portrait can make it almost unusable on such devices. Android is recently migrating to other types of devices as well. It is best to just let the device choose the preferred orientation.

Solution 4 - Android

It might be.. you have to identify it is tablet or phone by programmatically... First check device is phone or tablet

Determine if the device is a smartphone or tablet?

Tablet or Phone - Android

Then......

if(isTablet)
{
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);      
}else
{
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Solution 5 - Android

<activity android:name=".yourActivity"
          android:screenOrientation="portrait" ... />

add to main activity and add

android:configChanges="keyboardHidden"

to keep your program from changing mode when keyboard is called.

Solution 6 - Android

Set the Screen orientation to portrait in Manifest file under the activity Tag.

Here the example

You need to enter in every Activity

Add The Following Lines in Activity

for portrait

android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"

for landscape

android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"
Here The Example of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.thcb.app">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity"
            android:screenOrientation="portrait"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<activity android:name=".MainActivity2"
            android:screenOrientation="landscape"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Solution 7 - Android

Set the Screen orientation to portrait in Manifest file under the activity Tag.

Solution 8 - Android

android:screenOrientation="locked"

in <application> for all app in <activity> for actual activity

Solution 9 - Android

I would like to add to Bhavesh's answer. The problem is that if users keep the phone in landscape mode and run the app it will first go to landscape mode since it's based on sensor in manifest and will then immediately switch to portrait mode in phones because of the code in onCreate. To solve this problem below approach worked for me.

1 Declare locked orientation in manifest for activity android:screenOrientation="locked"

  <activity
        android:name=".overview.OverviewActivity"
        android:screenOrientation="locked" />

2 Check for tablet or phone in actiivty or base activity

override fun onCreate(savedInstanceState: Bundle?) {
    //check if the device is a tablet or a phone and set the orientation accordingly
    handleOrientationConfiguration()
    super.onCreate(savedInstanceState)
}

/**
 * This function has to be called before anything else in order to inform the system about
 * expected orientation configuration based on if it is a phone or a tablet
 */
private fun handleOrientationConfiguration() {
    requestedOrientation = if (UIUtils.isTablet(this).not()) {
        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
    } else {
        ActivityInfo.SCREEN_ORIENTATION_SENSOR
    }
}

UIUtils.kt

fun isTablet(context: Context): Boolean {
        return context.resources.configuration.smallestScreenWidthDp >= 600
    }

That's it, it will launch the app in the locked mode so that if you are on phone it will be always portraited and if you are on a tablet it will rotate based on the orientation of the device. This way it will eliminate the issue on phones where it switches between landscape and portrait at the start.

Read more here about the locked mode below https://developer.android.com/guide/topics/manifest/activity-element

Solution 10 - Android

Just Add:

android:screenOrientation="portrait"

in "AndroidManifest.xml" :

<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="@string/app_name">
</activity>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnkit RawatView Question on Stackoverflow
Solution 1 - AndroidRobinHoodView Answer on Stackoverflow
Solution 2 - AndroidHalim QarroumView Answer on Stackoverflow
Solution 3 - AndroidRadu SimionescuView Answer on Stackoverflow
Solution 4 - AndroidBhavesh HirparaView Answer on Stackoverflow
Solution 5 - AndroidSumodh SView Answer on Stackoverflow
Solution 6 - AndroidChetanView Answer on Stackoverflow
Solution 7 - AndroidRavish kumarView Answer on Stackoverflow
Solution 8 - AndroidAnton ShikhertView Answer on Stackoverflow
Solution 9 - Androidvikas kumarView Answer on Stackoverflow
Solution 10 - Androidhimanshu jainView Answer on Stackoverflow