Lock Android phone application to Portrait mode

AndroidPortraitAndroid Screen

Android Problem Overview


Can someone tell me how to lock my application to a portrait mode? Is it a simple configuration in the manifest file?

Android Solutions


Solution 1 - Android

Yes. Add android:screenOrientation="portrait" to the manifest under your main activity.

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

Solution 2 - Android

Yes! It's an attribute of the activity tag:

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

Solution 3 - Android

Also, you may have to add the following to your activity element:

android:configChanges="keyboardHidden"

That way, the OS won't change the orientation when the user opens a sliding keyboard.

Solution 4 - Android

None of these answers worked on my system but I did find the following worked perfectly for a simple app that I developed:

Within MainActivity.java add:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

to onCreate ()

This is mine:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

I know that it is not (always) best practice locking orientation, but in special circumstances it is valid and I only want this temporarily while I continue developing.

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
QuestionsexitrainerView Question on Stackoverflow
Solution 1 - Androidtechi.servicesView Answer on Stackoverflow
Solution 2 - AndroidFrancesco LauritaView Answer on Stackoverflow
Solution 3 - AndroidTim MahoneyView Answer on Stackoverflow
Solution 4 - AndroidNedView Answer on Stackoverflow