tags:

views:

5348

answers:

4

Hi,

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

Thanks.

+12  A: 

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

Modifying manifest

Open manifest, switch to Application tab and select desired Activity you wish to override 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 default layout.
  2. Select events for Config changes you wish to override: In this case these are keyboardHidden and orientation.

Modifying Activity implementation

Now you need to override single function within desired Activity.

Just add below function to your Activity's class.

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

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

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

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

(based on SDK 1.1)

Marcin Gil
Doesn't simply calling through to the superclass not change anything at all?
Matthias
Well.. I've just gave a recipe that works for me.
Marcin Gil
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
+10  A: 

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), 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 here: http://www.devx.com/wireless/Article/40792

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

haseman
This is the only answer that worked for me with Android 2.2. Thanks!
Zach Rattner
+2  A: 

You can define your activity in AndroidManifest.xml 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 inline solution for all application.

FeelGood
+1  A: 

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