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.
-
2@CommonsWare : My app has menus with long lists which is much more efficiently traversed in potrait mode on phones. – Ankit Rawat Mar 30 '13 at 13:15
-
3Then come up with a better GUI design, one that respects your users' ability to use their devices in any orientation. – CommonsWare Mar 30 '13 at 13:17
10 Answers
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.

- 37
- 1
- 3
- 12

- 10,897
- 4
- 48
- 97
-
22Or `sensorPortrait` if you want the user to be able to rotate the screen by 180 degrees :) – Eduard Luca Sep 02 '14 at 09:27
-
2
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" />
.

- 19,772
- 14
- 85
- 111

- 13,985
- 4
- 46
- 71
-
6+1 for the good practice quote and because this should be the accepted answer, as it's correct and was posted first... – Eduard Luca Sep 02 '14 at 09:29
-
7
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.

- 15,848
- 18
- 99
- 158

- 4,518
- 1
- 35
- 34
-
For those curious about the setting "nosensor" you can read more in the official docs at https://developer.android.com/guide/topics/manifest/activity-element – James Jordan Taylor May 14 '20 at 15:52
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?
Then......
if(isTablet)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

- 22,255
- 15
- 63
- 104
-
1
-
-
You can see edited answer. I have included how to determine if the device is a smartphone or tablet? – Bhavesh Hirpara May 20 '20 at 13:29
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>
-
i think create 2 class for one activity is redundant, sometime 1 class can handle orientation well but you tech other to duplicate this class – famfamfam Jul 29 '21 at 02:20
-
Worked for Emulators (Android 6 - Phone, Android 8.1 - Tablet, Android 6 - Tablet) with compile SDK v30 – Amil Waduwawara Aug 19 '21 at 03:52
android:screenOrientation="locked"
in <application>
for all app
in <activity>
for actual activity

- 11
- 1
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

- 10,447
- 2
- 46
- 52
Just Add:
android:screenOrientation="portrait"
in "AndroidManifest.xml" :
<activity
android:screenOrientation="portrait"
android:name=".MainActivity"
android:label="@string/app_name">
</activity>

- 1,549
- 1
- 15
- 27

- 29
- 3