How do I set it so the application is running in portrait mode only? I want the landscape mode to be disabled while the application is running. How do I do it programmatically?
18 Answers
For any Android version
From XMLYou can specify android:screenOrientation="portrait"
for each activity in your manifest.xml file. You cannot specify this option on the application
tag.
Other option is to do it programmatically, for example in an Activity
base class:
@Override
public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
For Android 4+ (API 14+)
Last option is to do it with activity lifecycle listeners which is only available since Android 4.0 (API 14+). Everything happens in a custom Application
class:
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleAdapter() {
@Override
public void onActivityCreated(Activity a, Bundle savedInstanceState) {
a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
}
ActivityLifecycleAdapter
is just a helper class you'll need to create which will be an empty implementation of ActivityLifecycleCallbacks
(so you don't have to override each and every methods of that interface when you simply need one of them).

- 28,208
- 16
- 81
- 124
-
1it is not working. it turns to landscape mode when i turned the device. and more thing in code i am using these setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); instructions for the progress bar. is any problem with these instructions? – naresh Jul 19 '11 at 11:04
-
it's working. one more thing don't put above two instructions in your coding part. it means it enables the sensor for changing the screen orientation. – naresh Jul 19 '11 at 11:31
-
3-1: The asker specifically requested how to do it //programmatically//, and for the entire application (which leads me to believe they don't want to do it for each activity individually). – jwir3 Aug 08 '14 at 15:59
-
@jwir3, I disagree. "programming" isn't just writing code code, it's also all of the meta files, resources you use, working around idiosyncrasies of the IDE, etc. This is a good answer, fixes the issue *and* the OP marked it as correct so was apparently a sufficient answer for the original question. – James Webster Jul 06 '15 at 14:44
-
Edited the answer to make it exhaustive. – Vincent Mimoun-Prat Jul 11 '15 at 09:04
-
9using the LifeCycleCallbacks method you will have a delay in activity launches when you hold your device in (what would normaly be) landscape mode. I guess it's because the devices returns to landscape mode briefly inbetween the activities and needs time to handle this. Setting orientation in manifest doesn't have this effect. – Anonymous Nov 20 '15 at 07:22
-
@Anonymous That's true, maybe setting orientation for each Activity is a tedious work, however, it is the safest way out. – S1ngoooor Mar 13 '17 at 22:51
-
Setting android:screenOrientation="portrait" in the manifest is the best practice since you can set it in the main activity and you won't need to repeat it for each activity. – XIII Oct 06 '17 at 18:51
-
How to setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); always. If I went out from app ex: settings screen, App changed portrait mode when auto rotation off only API version 27 and above. How to fix this issue? – Sakthivel Appavu Feb 25 '19 at 15:02
Yes you can do this both programmatically and for all your activities making an AbstractActivity that all your activities extends.
public abstract class AbstractActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
This abstract activity can also be used for a global menu.

- 14,468
- 8
- 37
- 37
-
4Note: I believe that `ActivityInfo.SCREEN_ORIENTATION_NOSENSOR` should be the better answer because it is intended to leave the device in the default state because some devices do not have a Portrait orientation. However, 'nosensor' does NOT work for me, so @arcone's answer stands for me. – David Manpearl Jun 29 '14 at 14:07
You can do this for your entire application without having to make all your activities extend a common base class.
The trick is first to make sure you include an Application
subclass in your project. In its onCreate()
, called when your app first starts up, you register an ActivityLifecycleCallbacks
object (API level 14+) to receive notifications of activity lifecycle events.
This gives you the opportunity to execute your own code whenever any activity in your app is started (or stopped, or resumed, or whatever). At this point you can call setRequestedOrientation()
on the newly created activity.
class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// register to be informed of activities starting up
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity,
Bundle savedInstanceState) {
// new activity created; force its orientation to portrait
activity.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
....
});
}
}

- 60,055
- 21
- 138
- 179
-
-
10this is the only answer which addresses the question. One line to rule them all. – Samuel May 08 '15 at 04:18
-
2
-
Damn I didn't even know such a thing was possible in Android! Awesome!!! Very handy. – Micro Mar 19 '16 at 19:13
-
5and never forget to add in manifest application root . android:name=".MyApp" – Faisal Naseer May 29 '16 at 02:47
-
Seems like a good option. Is there any disadvantage in using this approach – Shubham AgaRwal Jul 05 '16 at 14:15
-
3
-
4The problem is when you hold device horizontally and start new activity, it appears in landscape orientation and then rotates to portrait. Looks like the only good way to lock full app in portrait orientation is to mark every acitivity in Manifest. – Den May 31 '19 at 05:58
-
1@Den check this answer https://stackoverflow.com/a/50749081/1852397 for your problem when you hold device horizontally and start new activity, it appears in landscape orientation and then rotates to portrait. – cosic Mar 11 '20 at 15:50
-
i am new. can anyone share this full code? when i am trying to apply this then getting errors. so i told. – pankaj Mar 26 '20 at 19:43
You can set this in your manifest file..
android:name=".your launching activity name"
android:screenOrientation="portrait"
and you can also achive the same by writing the code in your class file like:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

- 2,832
- 4
- 45
- 72
-
1@ naresh pls accept ans if u got any solution according to your need it will race your rating also – SRam Jul 19 '11 at 10:41
-
This is really an easy (working) solution compared to the other solutions. It works for me with Kotlin. – HelloWorld May 30 '21 at 05:54
Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml. For example:
<activity android:name=".SomeActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">

- 6,579
- 8
- 53
- 86

- 21,853
- 23
- 89
- 133
Use:
android:screenOrientation="portrait"
Just write this line in your application's manifest file in each activity which you want to show in portrait mode only.

- 30,738
- 21
- 105
- 131

- 4,999
- 5
- 51
- 61
Write this to your manifest file, for every activity:
android:screenOrientation="portrait"

- 30,738
- 21
- 105
- 131

- 1,605
- 4
- 21
- 37
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting screen orientation locked so it will be acting as potrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}

- 707
- 9
- 27
As from Android developer guide :
"orientation" The screen orientation has changed — the user has rotated the device. Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.
"screenSize" The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device). Added in API level 13.
So, in the AndroidManifest.xml file, we can put:
<activity
android:name=".activities.role_activity.GeneralViewPagerActivity"
android:label="@string/title_activity_general_view_pager"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|screenSize"
>
</activity>

- 629
- 1
- 10
- 17
Adding <preference name="orientation" value="portrait" />
under <widget>
in my config.xml worked for me.
(The other solutions either didn't work on my device, were overwritten during building or gave deprecation errors during the build process.)

- 2,888
- 2
- 24
- 36
-
Where is the said config.xml? What development platform are you referring to? – Tash Pemhiwa Jun 10 '16 at 06:59
in Manifest file which activity you want to use in "portrait" you must write these code in Activity tag
android:screenOrientation="portrait"
like this
android:icon="@drawable/icon"
android:name="com.zemkoapps.hd.wallpaper.AndroidGridLayoutActivity"
android:screenOrientation="portrait" >
but if u want screen in landscape use this code like this
android:screenOrientation="landscape"

- 9
- 1
If anyone was wondering , how you could do this for your entire application without having to make all your activities extend a common base class in Kotlin , see the example below :
class InteractiveStoryApplication: Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(object: ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
override fun onActivityPaused(activity: Activity?) {
}
override fun onActivityResumed(activity: Activity?) {
}
override fun onActivityDestroyed(activity: Activity?) {
}
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
}
override fun onActivityStarted(activity: Activity?) {
}
override fun onActivityStopped(activity: Activity?) {
}
})
}
}
and then you have to add your common base class in AndroidManifest like so:
<application android:allowBackup="true"
android:name=".InteractiveStoryApplication"

- 1,783
- 3
- 16
- 25
You can do it in two ways .
- Add
android:screenOrientation="portrait"
on your manifest file to the corresponding activity - Add
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
to your activity in `onCreate() method

- 1,796
- 16
- 23
Similar to Graham Borland answer...but it seems you dont have to create Application class if you dont want...just create a Base Activity in your project
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
And extend this class instead of AppCompatActivity where you want to use Potrait Mode
public class your_activity extends BaseActivity {}

- 59
- 4
For Xamarin Users:
If you extends all your activities to a BaseActivity
Just add:
this.RequestedOrientation = ScreenOrientation.Portrait;
This will resolve the problem. If you want any particular activity to be in landscape override this in OnActivityCreated
. As:
this.Activity.RequestedOrientation = ScreenOrientation.Landscape;
Well, I tried every answer but it didn't work in older versions of android. So, the final solution is to add this code to every activity just above setContentView:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

- 1
- 2
In kotlin -->
Use this in your Extends Application class fun onCreate()...
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityCreated(p0: Activity, p1: Bundle?) {
p0.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
}
override fun onActivityStarted(p0: Activity) {
}
override fun onActivityResumed(p0: Activity) {
}
override fun onActivityPaused(p0: Activity) {
}
override fun onActivityStopped(p0: Activity) {
}
override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
}
override fun onActivityDestroyed(p0: Activity) {
}
}
)}

- 705
- 7
- 8
In your Manifest type this:
<activity
android:screenOrientation="portrait"
<!--- Rest of your application information ---!>
</activity>

- 716
- 9
- 21

- 1
- 2
-
6This doesn't add anything new that 3 year old answers haven't already said. You might consider deleting it. If you decide to edit your answer to include more information that distinguishes your answer from others, please @reply to me. – Artjom B. Jul 31 '14 at 15:08
-
1android:screenOrientation="portrait" can not be added to application tag, it must go inside activity tag – Hossein Amini Oct 09 '15 at 05:05