Home / DeveloperSection / Blogs / Locking Activity Orientation in Android

Locking Activity Orientation in Android

Anonymous User3917 18-Feb-2015

In my previous articles, we analyze the tweaks involved with action bar:

Now we see how to lock activity orientation 

A certain activity in your application should not be allowed to rotate, or rotation requires more-direct intervention from the application code.

Using static declarations in the AndroidManifest.xml file, we can modify each individual activity to lock into either portrait or landscape orientation.

This can be applied only to the <activity> tag, so it cannot be done once for the entire application scope.

Simply add:

·      android:screenOrientation="portrait" or

·       android:screenOrientation="landscape"

to the <activity> element, and the activity will always display in the specified orientation, regardless of how the device is positioned.

There is also an option you can pass in the XML entitled behind. If an activity element has:


 set, it will take its settings from the previous activity in the stack. This can be a useful way for an activity to match the locked orientation of its originator for some slightly more dynamic behavior.

Updating AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="com.example.msclient010.actionbarapplicationtest" >
            <!-- To support below api level 14 -->
            <meta-data android:name=""
                android:value="splitActionBarWhenNarrow" />
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


 Run the Application for Portrait locking 


The application will start in Portrait mode by default :

Locking Activity Orientation in Android


Turn the screen orientation to Landscape, notice the UI will still stick to portrait mode


Locking Activity Orientation in Android


 Run the application for Landscape locking: 

Now, we lock the screen for landscape mode, for this just change the value of android:screenOrientation  to "landscape" :


The application will run in landscape mode by default

Locking Activity Orientation in Android


Now change the orientation to portrait mode, the UI is still stick to landscape mode


Locking Activity Orientation in Android


Thanks for reading this post.

Happy Coding!!J


Updated 18-Feb-2015
I am a content writter !

Leave Comment


Liked By