blog

Home / DeveloperSection / Blogs / Locking Activity Orientation in Android

Locking Activity Orientation in Android

Anonymous User3976 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:

android:screenOrientation="behind"

 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="http://schemas.android.com/apk/res/android"
    package="com.example.msclient010.actionbarapplicationtest" >
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:uiOptions="splitActionBarWhenNarrow"
            android:screenOrientation="portrait">
            <!-- To support below api level 14 -->
            <meta-data android:name="android.support.UI_OPTIONS"
                android:value="splitActionBarWhenNarrow" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

 

 Run the Application for Portrait locking 

android:screenOrientation="portrait"

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" :

android:screenOrientation="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

Comments

Liked By