Android Screen Orientation Lock Tutorial

In this tutorial, we are going to learn how to implement android screen orientation lock. It is important for us to understand why we might want to lock a screen to a particular orientation in android.

In android devices, running app may be view in a portrait or landscape form when you rotate your device.
There are few reasons why you may want to lock your screen orientation when developing android application. For instance, menu or intro activity pages are sometimes fix to a particular screen orientation. Another important example is in games. Since landscape mode afford more space in most android device, game play screen is always fix to landscape.

Now that you have a little idea about why you may lock your application screen orientation in android, we will move on to demonstrate how we can achieve this in android. First we will learn how android handles orientation or screen configuration changes internally. When a device rotates and there is a configuration change in screen orientation, android system will kill the activity and recreate another new activity layout from the previous one but in different orientation.

The natural orientation change does not cause any problem in applications that are designed for it but in some applications, it might result in a crash. This mainly occur when android system loses some previous data when it changes to a different orientation.

If you want to maintain the same state in your android during orientation change, you have to override onSaveInstanceState() to save some run-time states in a bundle. The new activity will access the information from the bundle which is pass as a parameter in the onCreate callback function of the activity class.

Android screen orientation can be accomplish from android configuration file or programmatically. We will go into details with the two processes.

I use the following environment and tools in this tutorial but you are free to use whatever tools you are familiar with.

Windows 7
Android Studio
Samsung Galaxy Fame Lite
Min SDK 8
Target SDK 19

There is no better time to start creating our application than now. To create a new android application project, following the steps as stipulated below.

Go to File menu
Click on New menu
Click on Android Application
Enter Project name: LockScreenOrientation
Package: com.tutorial.lockscreenorientation
Keep other default selections.
Continue to click on next button until Finish button is active
Then click on Finish Button

Below is a visual representation of how to create a new android application.
screenlock
Once you are done with your application project creation, open your application manifest.xml file located in your project root directory. Copy and paste the following code to the file as shown below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inducesmile.lockscreenorientation.lockscreenorientation" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

In the above code, you will see that on line 12, we added the configuration change attribute to our manifest file. It will instruct the android system that we will like to handle the screen configuration change of our application by ourselves. That is the only code you need to achieve this. I heard you say it is so simple. Yes, it is.

Now, we will move ahead to demonstrate how this can be achieved programmatically. Open the MainActivity.java file in the src folder of your project. In the onCreate callback function set the request orientation and pass the ActivityInfo flag you want to use. The final piece of code is shown below.

package com.inducesmile.lockscreenorientation.lockscreenorientation;
import android.content.pm.ActivityInfo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Just like I previously said before, once you use setRequestedOrientation() in your activity page, android system will not invoke the onConfigurationChanged() function which alerts your activity about configuration change since it knows you have decided to handle it yourself.

If you have questions or suggestions, kindly use the comment box below. You can also download the complete source code for this application here.

Add a Comment