Saturday, June 2, 2012

Disable Android Screen Rotation/Orientation

Some time you need to Disable or Lock the screen orientation.

just need to add this code in your activity which you want to lock


//for Portrait Mode only
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                                                           or
//for Landscape Mode only
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

well its pretty simple to achieve this, still i have created a small app
-------------------------------------------
App Name: DisableScreenOrientation
Package Name: com.rdc
Android SDK: Android SDK 2.3.3 / API 10
Default Activity Name: MyActivity
-------------------------------------------
so here is the code

MyActivity.java

package com.rdc;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//if you want to lock screen for always Portrait mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

//if you want to lock screen for always Landscape mode
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}
}

main.xml



and the manifest file is
 AndroidManifest.xml













Tested on Real Androd Device [Sony Ericsson (XPERIA)] the output will be like this..


I'd love to hear your thoughts!  

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home