Java tutorial
//package com.java2s; //License from project: LGPL import android.app.Activity; import android.content.pm.ActivityInfo; import android.graphics.Point; import android.os.Build; import android.view.Display; import android.view.Surface; public class Main { /** * @Thach Feb 21, 2014 * @Desc lock Orientation * @param b */ @SuppressWarnings("deprecation") public static void lockOrientation(Activity act, boolean isLock) { if (act == null) { return; } if (isLock) { Display display = act.getWindowManager().getDefaultDisplay(); int rotation = display.getRotation(); int height; int width; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) { height = display.getHeight(); width = display.getWidth(); } else { Point size = new Point(); display.getSize(size); height = size.y; width = size.x; } switch (rotation) { case Surface.ROTATION_90: if (width > height) act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); else act.setRequestedOrientation(9/* reversePortait */); break; case Surface.ROTATION_180: if (height > width) act.setRequestedOrientation(9/* reversePortait */); else act.setRequestedOrientation(8/* reverseLandscape */); break; case Surface.ROTATION_270: if (width > height) act.setRequestedOrientation(8/* reverseLandscape */); else act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; default: if (height > width) act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); else act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } else { act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } } }