How to make an application ignore screen orientation change?

Android

Android Problem Overview


Is there a way to make an application completely ignore a screen orientation change?

Android Solutions


Solution 1 - Android

It is possible, quite easily, to override the default behavior and forbid a screen orientation change when the keyboard is open/closed.

Modifying the manifest

Open the manifest, switch to the Application tab and select the desired Activity you wish to override for the orientation change behavior.

  1. Within Attributes you need to change two fields: Screen orientation: select either portrait or landscape - whichever is desired. This will be the default layout.

  2. Select events for Config changes you wish to override: In this case these are keyboardHidden and orientation.

Modifying the Activity implementation

Now you need to override a single function within desired Activity.

Just add the function below to your Activity's class.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

This is the default implementation if using the Source->Override/Implement Methods menu option.

That's it! Now your orientation will always be kept.

Remember that this setting is per Activity - so you need to repeat this step for each Activity you wish to forbid the orientation change!

(Based on SDK 1.1)

Solution 2 - Android

You can make the same change in code with the following line (called in an activity):

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Once you make this call, your application will stay in landscape (or portrait) mode. You can use the same call (with a different ActivityInfo enum) to make it sensitive to the orientation shifting again.

There's a full DevX article on the topic in Developing Orientation-Aware Android Applications.

(WARNING: since I've posted this link DevX has put up a registration wall.)

Solution 3 - Android

If you are setting either by AndroidManifest.xml or with the setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); you are going to run into issues with tablets. Their natural/default orientation is landscape.

If you truly want to completely ignore screen orientation changes I would use this setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); value. I talk more about it in Stack Overflow question Android natural sensor orientation help.

Here is the xml:

<activity 
    android:name=".MyActivity" 
    android:screenOrientation="nosensor"
    android:configChanges="orientation|keyboardHidden|keyboard"/>

Solution 4 - Android

You can define your activity in the AndroidManifest.xml file like this:

<activity 
    android:name=".MyActivity" 
    android:screenOrientation="portrait"
    android:configChanges="orientation|keyboardHidden|keyboard"/>`

In this case you should set the property for each activity. I didn't find an inline solution for all applications.

Solution 5 - Android

<activity  android:screenOrientation="portrait"></activity>

Solution 6 - Android

Add this to the activity:

android:configChanges="orientation|screenSize"

Solution 7 - Android

You want to read the current orientation and keep it this way throughout all the activity's lifetime, so what I did is the following, at the end of onCreate:

// choose an orientation and stay in it
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
   	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
   	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

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
QuestionLyubomyr DutkoView Question on Stackoverflow
Solution 1 - AndroidMarcin GilView Answer on Stackoverflow
Solution 2 - AndroidhasemanView Answer on Stackoverflow
Solution 3 - AndroidbytebenderView Answer on Stackoverflow
Solution 4 - AndroidFeelGoodView Answer on Stackoverflow
Solution 5 - Androiduser1103138View Answer on Stackoverflow
Solution 6 - AndroidDon LarynxView Answer on Stackoverflow
Solution 7 - AndroidMicView Answer on Stackoverflow