views:

77

answers:

3

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

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

A: 

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 http://stackoverflow.com/questions/1512045/how-to-disable-orientation-change-in-android

mdma
I tied the following:1) in manifest:<activity android:name="AboutActivity" android:configChanges="keyboardHidden|orientation"/>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
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
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
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
A: 

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

androidworkz
If I'd add this line, would it work for all application? or it would work on for the activity?
davs
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
A: 

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