68

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

2
  • Just one note that apparently in Android 1.5 there is a bug. If you use a custom screen size (e.g. 800x480, like on the Apad) and force it to landscape (either via XML or from the code) then it will be in fact in portrait.
    – Dornbi
    Commented Oct 1, 2010 at 9:04
  • You may just call Screen.lockOrientation(this) from github.com/delight-im/Android-BaseLib/blob/master/Source/src/im/… if you want to lock and unlock the orientation from code (which is more flexible)
    – caw
    Commented Mar 4, 2015 at 18:33

7 Answers 7

58

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)

4
  • 16
    Doesn't simply calling through to the superclass not change anything at all?
    – mxk
    Commented Sep 11, 2009 at 13:05
  • 10
    I was just playing around with something very similar - I think the changes to the Manifest file are sufficient, at least they were for me (which seems logical, as Matthias pointed out).
    – alex_c
    Commented Sep 11, 2009 at 16:28
  • to change in manifest is sufficient to lock the orientation, then what is the purpose of onConfigurationChanged()? Commented Jun 24, 2011 at 4:46
  • As far for my tests: if you'll just change the manifest, without overriding the onConfigurationChanged function, the onCreate() and the whole activity lifecycle events will be invoked. If You'll change the manifest AND override the onConfigurationChanged function, it won't be called.
    – ofirbt
    Commented Jul 5, 2011 at 16:12
28

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.)

1
  • This is the only answer that worked for me with Android 2.2. Thanks! Commented Sep 10, 2010 at 3:45
21

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"/>
3
  • This won't rotate the screen.
    – Don Larynx
    Commented May 21, 2015 at 12:37
  • 1
    I'm not sure I follow... That is the point, to avoid rotating the screen.
    – bytebender
    Commented May 27, 2015 at 17:18
  • "completely ignore screen orientation changes" means that when rotated, the data will remain the same.
    – Don Larynx
    Commented May 27, 2015 at 19:54
14

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.

3

Add this to the activity:

android:configChanges="orientation|screenSize"

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

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.