I want my android application to be only run in portrait mode? How can I do that?
10 Answers
In the manifest, set this for all your activities:
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
Let me explain:
- With
android:configChanges="orientation"
you tell Android that you will be responsible of the changes of orientation. android:screenOrientation="portrait"
you set the default orientation mode.

- 198,401
- 62
- 356
- 264
-
56Wouldn't be great If we could set a "global" orientation for ALL activities in the Application? Looks like we have no option but copy/paste the "portrait" attribute on every activity... – Eduardo Coelho Apr 18 '13 at 21:58
-
81It's enough to set `android:screenOrientation="portrait"` – Till - Appviewer.io Aug 29 '13 at 11:35
-
12@EduardoCoelho you can actually make a base class `PortraitActivity` and in onCreate call `setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)` All activities extending this won't rotate – Twinone Jun 06 '14 at 09:07
-
2@Twinone: How about when different activities want to inherit from different subclasses of `Activity`, like one activity extends from `ListActivity` while others extend simply from `Activity`? – RestInPeace Jun 15 '14 at 21:50
-
3@RestInPeace Yeah, good point. But that's not my idea being wrong, that's just Java not supporting multiple inheritance :) – Twinone Jun 16 '14 at 09:00
-
1@Till wouldn't be best practices to also set the configChanges? – Gabriel Augusto Jun 23 '16 at 13:25
-
how about that add this setting to app's theme? – Tommy Mar 12 '19 at 03:22
-
1Note that `android:screenOrientation` is ignored in multi-window mode https://developer.android.com/guide/topics/manifest/activity-element#screen – Ewan May 31 '19 at 15:22
-
3`tools:ignore="LockedOrientationActivity"` - Add this to disable warning message in Manafest.xml. – Sattar Hummatli Apr 05 '20 at 13:22
In Android Manifest File, put attribute for your <activity>
that android:screenOrientation="portrait"

- 90,477
- 74
- 177
- 219
-
2let me to add: from Android Sudio 3.6 need to use android:screenOrientation="fullSensor" or android:screenOrientation="unspecified". fullSensor Means either you have ON the "Rotate off" or not it will change the orientation based on you move the phone. unspecified Means if you have ON the Rotate off then it will stay only in that orientation and if not then it will change the orientation based on you move the phone. – Kirguduck Apr 05 '20 at 09:21
There are two ways,
- Add
android:screenOrientation="portrait"
for each Activity in Manifest File - Add
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
in each java file.

- 5,491
- 5
- 48
- 71

- 303
- 3
- 7
-
20The second option has the horrible side effect that it will restart your activity when started in in Portrait. – Joakim Apr 29 '16 at 13:04
in the manifest:
<activity android:name=".activity.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>
or : in the MainActivity
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

- 1,141
- 12
- 14
-
I get `app/src/main/AndroidManifest.xml; lineNumber: 20; columnNumber: 50; The prefix "tools" for attribute "tools:ignore" associated with an element type "activity" is not bound.`. Adding `xmlns:tools="http://schemas.android.com/tools"` in the root element solves the problem – Żabojad Apr 02 '20 at 15:21
Old post I know. In order to run your app always in portrait mode even when orientation may be or is swapped etc (for example on tablets) I designed this function that is used to set the device in the right orientation without the need to know how the portrait and landscape features are organised on the device.
private void initActivityScreenOrientPortrait()
{
// Avoid screen rotations (use the manifests android:screenOrientation setting)
// Set this to nosensor or potrait
// Set window fullscreen
this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = new DisplayMetrics();
this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
// Test if it is VISUAL in portrait mode by simply checking it's size
boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels );
if( !bIsVisualPortrait )
{
// Swap the orientation to match the VISUAL portrait mode
if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
{ this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); }
}
else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); }
}
Works like a charm!
NOTICE:
Change this.activity
by your activity or add it to the main activity and remove this.activity
;-)

- 6,501
- 6
- 57
- 99
Alternative solution is to set activity's orientation using ActivityLifecycleCallbacks
.
import android.app.Activity
import android.app.Application
import android.content.pm.ActivityInfo
import android.os.Bundle
class App : Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
}
override fun onActivityStarted(activity: Activity) {}
override fun onActivityResumed(activity: Activity) {}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, savedInstanceState: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {}
})
}
}

- 5,081
- 5
- 39
- 61
Try this: (if SDK 23 & above)
Add your AndroidManifest.xlm
;
<activity android:name=".YourActivity"
android:screenOrientation="locked"/>
like this.

- 3,208
- 9
- 22
- 33
in new SDk
of android (24 and above)
you can use in Manifest.xml
in activity
tag as you want:
<activity
android:name=".feature.main.MainActivity"
********* android:screenOrientation="locked" ******
android:configChanges="uiMode"
android:windowSoftInputMode="adjustPan"
android:exported="true">
and it work!

- 6,656
- 2
- 44
- 44
The accepted answer is correct. However, if you happen to be doing cross-platform development in C# using Uno Platform the Activity configuration is defined in an Activity attribute in the class. I'd expect this is the same if you're doing Xamarin or Xamarin Forms development.
Set: ScreenOrientation = ScreenOrientation.Portrait in the attribute parameters.
Here's an example within my MainActivity.cs class-level attribute:
[Activity(MainLauncher = true,
ConfigurationChanges = global::Uno.UI.ActivityHelper.AllConfigChanges,
WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden,
ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : Windows.UI.Xaml.ApplicationActivity { }
#unoplatform #xamarin #csharp #windows #crossplatform

- 1,303
- 10
- 28
I use
android:screenOrientation="nosensor"
It is helpful if you do not want to support up side down portrait mode.

- 784
- 8
- 18
-
if this does what I think it does, it doesn't really force the screen to portrait, which you might want to do. – Lassi Kinnunen Nov 21 '14 at 09:27
-
why don't you just remove this comment ! it doesn't make sense – Alexander Zaldostanov Nov 02 '15 at 10:51
-
In Android Manifest File just add attribute for your
that android:screenOrientation="portrait" – Avinash Shinde Jan 13 '20 at 05:30 -