Android : Lock Screen Orientation

This is another trivial tip. Some application screen demands to lock the orientation in either Portrait or Landscape mode.
There are two ways to do this in the android. Depending on specific requirements the approach can be decided.

From the configuration

This is obvious approach where the setting can be done in the AndroidManifest.xml of the project. Below is the configuration.

<application android:icon="@drawable/icon" android:label="@string/app_name">
	<activity android:name=".MainActivity"
		<action android:name="android.intent.action.MAIN" />
		<category android:name="android.intent.category.LAUNCHER" />
	<activity android:name=".Activity1"
	<activity android:name=".Activity2"

This is standard setting of the activity in AndroidManifest.xml. Note the lines #4, #12 and #16 in the above code snippet. By adding attribute for the activity tag, the screen contents will be always displayed in the portrait mode. This line needs to be added to all those activities which needs to be displayed in portrait mode. Change the above string value to landscape to lock the orientation in landscape mode for a given activity.

Programmatic Approach

This is another way to achieve the same from the code. Add following code in the activity’s onCreate(Bundle savedInstanceState) method.

public void onCreate(Bundle savedInstanceState) {

Line #3 in the above code snippet sets the screen orientation to landscape for a given activity. Using programmatic approach allows developer to optionally lock the screen orientation by checking some condition if needed.

Function setRequestedOrientation(int requestedOrientation) takes int parameter which is ActivityInfo.screenOrientation. List of possible values for this parameter can be seen here.


Posted on March 30, 2012, in Android and tagged , , . Bookmark the permalink. 6 Comments.

  1. Hi Ajit, congrats on your first game in the Play Store. Have you shared it on G+? (I can’t find you there). I’d be glad to reshare it and make a few comments on how it could be improved.

  2. this works for user defined activities…. For Example I am working on Class
    I am setting the orientation to portrait…. If i change the emulator screen to landscape … this activity is changed to landscape. but i want it to remain in portrait only…. can u pls help me in dis issue……………

  3. Where do I find AndroidManifest.xml?

    • It’s in the root of your android project.

      • Hello,
        Thanks for your help, but as i am not developer so i don’t know about the android project and anything about android development.

        So can you please tell me how to do it from my phone. The. Already rooted my phone and i am using samsung galaxy Ace and i am using phone in landscape mode but only lockscreen is only app thah i couldn’t make landscape. So please tell me how to do that.

        Thanks its naren.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: