2

When I press a button, I would like to disable screen rotation on all my activities. How can I do that?

BTW, the phone can be located in landscape or portrait position when the user click the button.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
davs
  • 8,566
  • 8
  • 39
  • 53

3 Answers3

12

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

androidworkz
  • 2,872
  • 1
  • 16
  • 19
  • If I'd add this line, would it work for all application? or it would work on for the activity? – davs Jul 27 '10 at 11:41
  • It seems like it works only for the activity, where I invoke this code and only until I move to another activity (When I returns to this activity, defalut settings are applied).. I think it is no good practice to invoke such code (checking, should we rotate and rotate screen if we need) for all activities in onResume/onSatrt methods – davs Jul 27 '10 at 12:11
4

You can change your AndroidManifest.xml to

<activity android:name="MainActivity" android:configChanges="orientation">

Which informs the OS that you will handle these config changes (by doing nothing.)

See How do I disable orientation change on Android?

Community
  • 1
  • 1
mdma
  • 54,185
  • 11
  • 85
  • 125
  • I tied the following: 1) in manifest: 2) in AboutActivity: @Override public void onConfigurationChanged(Configuration newConfig) { newConfig.orientation = Configuration.ORIENTATION_LANDSCAPE; super.onConfigurationChanged(newConfig); } But anyway activity's screen rotates when device rotates. What did I wrong? – davs Jul 25 '10 at 13:50
  • 1
    You call super.onConfigurationChanges() for orientation changes too, which rotates the screen. Use an if structure to determine if the orientation/keyboard state has changed; you should call super only for other changes. – molnarm Jul 25 '10 at 17:20
  • It doesn't matter whether or not you call super. The configuration change has already happened at the time of this callback; it is just being called to tell you about it. – hackbod Jul 25 '10 at 18:40
  • I seem to recall that the framework throws an exception if you don't call `super` (even if, for this method, it has no particular effect). – Christopher Orr Jul 26 '10 at 02:14
3

I've found the solution:

in manifest.xml:

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar" android:name="MyApplication">

in MyApplication.java:

public class UPackingListApplication extends Application {

    public void onConfigurationChanged(Configuration newConfig) {
        newConfig.orientation = [orientation we wanna to use (according to ActivityInfo class)];
        super.onConfigurationChanged(newConfig);
    }


}
davs
  • 8,566
  • 8
  • 39
  • 53
  • I've tried this at the Activity level and it works in terms of setting the internal orientation tracking to whatever you set it to, but in terms of actually stopping the orientation change or being able to physically set the orientation, it doesn't seem to work. – Paul Mennega Feb 25 '11 at 21:04