How do I disable orientation change on Android?

AndroidAndroid ActivityAndroid Orientation

Android Problem Overview


I have an application that I just would like to use in portrait mode, so I have defined android:screenOrientation="portrait" in the manifest XML. This works OK for the HTC Magic phone (and prevents orientation changes on other phones as well).

But I have a problem with the HTC G1 phone as I open the hardware QWERTY keyboard (not the virtual keyboard). My activity stays in portrait mode, but it seems to get restarted and loses all its states. This does not happen with the HTC Hero version.

My application is quite big, so I don't want it to restart and lose all its states when the keyboard is opened. How can I prevent that?

Android Solutions


Solution 1 - Android

Update April 2013: Don't do this. It wasn't a good idea in 2009 when I first answered the question and it really isn't a good idea now. See this answer by hackbod for reasons:

https://stackoverflow.com/questions/5335665/avoid-reloading-activity-with-asynctask-on-orientation-change-in-android/5336057#5336057

Add android:configChanges="keyboardHidden|orientation" to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.

<activity android:name="MainActivity"
     android:screenOrientation="portrait"
     android:configChanges="keyboardHidden|orientation">

See Developer reference configChanges for more details.

However, your application can be interrupted at any time, e.g. by a phone call, so you really should add code to save the state of your application when it is paused.

Update: As of Android 3.2, you also need to add "screenSize":

<activity
    android:name="MainActivity"
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden|orientation|screenSize">

From Developer guide Handling the Configuration Change Yourself

> Caution: Beginning with Android 3.2 (API level 13), the "screen size" > also changes when the device switches between portrait and landscape > orientation. Thus, if you want to prevent runtime restarts due to > orientation change when developing for API level 13 or higher (as > declared by the minSdkVersion and targetSdkVersion attributes), you > must include the "screenSize" value in addition to the "orientation" > value. That is, you must declare > android:configChanges="orientation|screenSize". However, if your > application targets API level 12 or lower, then your activity always > handles this configuration change itself (this configuration change > does not restart your activity, even when running on an Android 3.2 or > higher device).

Solution 2 - Android

You need to modify AndroidManifest.xml as Intrications (previously Ashton) mentioned and make sure the activity handles the onConfigurationChanged event as you want it handled. This is how it should look:

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

Solution 3 - Android

I've always found you need both

android:screenOrientation="nosensor" android:configChanges="keyboardHidden|orientation"

Solution 4 - Android

As said, set android:configChanges of your Activity (in manifest file) to keyboardHidden|orientation and then:

  1. Override onConfigurationChanged()

    @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); //here you can handle orientation change }

  2. Add this line to your activity's onCreate()

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

It's better than add same line to onConfigurationChanged, because your app will turn to portrait mode and then back to landscape (it will happen only one time, but it's annoying).

Also you can set android:screenOrientation="nosensor" for your activity (in manifest). But using this way you're a not able to handle orientation changes at all.

Solution 5 - Android

Use this..

    android:screenOrientation="portrait"

Solution 6 - Android

In OnCreate method of your activity use this code:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Now your orientation will be set to portrait and will never change.

Solution 7 - Android

In the AndroidManifest.xml file, for each activity you want to lock add the last screenOrientation line:

android:label="@string/app_name"
android:name=".Login"
android:screenOrientation="portrait" >

Or android:screenOrientation="landscape".

Solution 8 - Android

In your androidmanifest.xml file:

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

or

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

Solution 9 - Android

To lock the screen by code you have to use the actual rotation of the screen (0, 90, 180, 270) and you have to know the natural position of it, in a smartphone the natural position will be portrait and in a tablet, it will be landscape.

Here's the code (lock and unlock methods), it has been tested in some devices (smartphones and tablets) and it works great.

public static void lockScreenOrientation(Activity activity)
{	
	WindowManager windowManager =  (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);	
	Configuration configuration = activity.getResources().getConfiguration();	
	int rotation = windowManager.getDefaultDisplay().getRotation();	
		
	// Search for the natural position of the device	
	if(configuration.orientation == Configuration.ORIENTATION_LANDSCAPE &&	
	   (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) ||	
	   configuration.orientation == Configuration.ORIENTATION_PORTRAIT &&	
	   (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270))	
	{	
		// Natural position is Landscape	
		switch (rotation) 	
		{	
			case Surface.ROTATION_0: 	
				activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 	
				break;   	
			case Surface.ROTATION_90: 	
				activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
	        break;   	
		    case Surface.ROTATION_180: 
		    	activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
		        break;          
		    case Surface.ROTATION_270: 
		    	activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
		        break;
		}
	}
	else
    {
		// Natural position is Portrait
		switch (rotation) 
		{
			case Surface.ROTATION_0: 
				activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
	        break;   
			case Surface.ROTATION_90: 
				activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
	        break;   
		    case Surface.ROTATION_180: 
		    	activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
		        break;          
		    case Surface.ROTATION_270: 
		    	activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); 
		        break;
		}
    }
}

public static void unlockScreenOrientation(Activity activity)
{
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

Solution 10 - Android

In Visual Studio Xamarin:

  1. Add:

using Android.Content.PM; to you activity namespace list.

  1. Add:

[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]

as an attribute to you class, like that:

[Activity(ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : Activity
{...}

Solution 11 - Android

Add

android:configChanges="keyboardHidden|orientation|screenSize" 

to your manifest.

Solution 12 - Android

You can simply use like below in the application class if you want only PORTRAIT mode for all activities in your app.

class YourApplicationName : Application() {

override fun onCreate() {
    super.onCreate()

    registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {

        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }

        override fun onActivityStarted(activity: Activity) {

        }

        override fun onActivityResumed(activity: Activity) {

        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {

        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

        }

        override fun onActivityDestroyed(activity: Activity) {

        }

    })

}

}

Solution 13 - Android

Please note, none of the methods seems to work now!

In Android Studio 1 one simple way is to add android:screenOrientation="nosensor".

This effectively locks the screen orientation.

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
QuestionVidar VestnesView Question on Stackoverflow
Solution 1 - AndroidIntricationsView Answer on Stackoverflow
Solution 2 - AndroidubershmekelView Answer on Stackoverflow
Solution 3 - Androidm00seyView Answer on Stackoverflow
Solution 4 - AndroidDmitry ZaytsevView Answer on Stackoverflow
Solution 5 - AndroidMuhammad Aamir AliView Answer on Stackoverflow
Solution 6 - AndroidNumenorView Answer on Stackoverflow
Solution 7 - AndroidGil AllenView Answer on Stackoverflow
Solution 8 - AndroidJorgesysView Answer on Stackoverflow
Solution 9 - AndroidPoOkView Answer on Stackoverflow
Solution 10 - AndroidRoy DoronView Answer on Stackoverflow
Solution 11 - AndroidsaurabhView Answer on Stackoverflow
Solution 12 - AndroidElaa_sekarView Answer on Stackoverflow
Solution 13 - AndroidkephalianView Answer on Stackoverflow