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

11

107

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.

Pylos answered 30/3, 2013 at 13:7 Comment(2)
@CommonsWare : My app has menus with long lists which is much more efficiently traversed in potrait mode on phones.Pylos
Then come up with a better GUI design, one that respects your users' ability to use their devices in any orientation.Elzaelzevir
G
190

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.

Gaeta answered 30/3, 2013 at 13:11 Comment(3)
Or sensorPortrait if you want the user to be able to rotate the screen by 180 degrees :)Adelbert
how is this solving the second part of the question?Eldoria
It doesn't work. My tablet is still in portrait orientation as a phone... Can't believe so many upvotes for the misleading answer.Ragged
M
51

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" />.

Metastasis answered 30/3, 2013 at 13:9 Comment(0)
P
27

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 to the device's "preferred" orientation by using

    android:screenOrientation="nosensor"

This will lead to forcing your app to portrait on most 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.

Polytechnic answered 12/11, 2014 at 11:30 Comment(1)
For those curious about the setting "nosensor" you can read more in the official docs at developer.android.com/guide/topics/manifest/activity-elementWestcott
C
13

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);
}
Carmella answered 30/3, 2013 at 13:23 Comment(1)
Hi @Bhavesh, It's not working for Android 9 mobile.Harrier
B
3
<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.

Bugbane answered 30/3, 2013 at 13:13 Comment(0)
S
2

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>

Subsocial answered 9/5, 2020 at 11:12 Comment(1)
i think create 2 class for one activity is redundant, sometime 1 class can handle orientation well but you tech other to duplicate this classVarner
A
2

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

Ankylosis answered 20/4, 2022 at 10:30 Comment(0)
W
1

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

Weatherley answered 14/5, 2015 at 13:36 Comment(0)
M
1
android:screenOrientation="locked"

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

Muskogee answered 23/3, 2020 at 9:59 Comment(0)
P
0

Just add in AndroidManifest file

<activity android:name=".yourActivity"
      android:screenOrientation="behind" ... />
Pusan answered 27/2 at 8:37 Comment(0)
N
-2

Just Add:

android:screenOrientation="portrait"

in "AndroidManifest.xml" :

<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="@string/app_name">
</activity>
Nicolina answered 17/12, 2019 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.