Android examples for Activity:Activity Orientation
Locks the activity to the orientation the user was in when they entered the activity.
//package com.java2s; import android.app.Activity; import android.content.pm.ActivityInfo; import android.graphics.Point; import android.view.Display; import android.view.Surface; public class Main { /**// w w w.ja v a2 s . co m * Locks the activity to the orientation the user was in when they entered the activity. * Call in Activity.onCreate * Android, why you no have this? * @param activity activity to lock */ public static void lockToCurrentOrientation(Activity activity) { Display display = activity.getWindowManager().getDefaultDisplay(); int rotation = display.getRotation(); int height; int width; Point size = new Point(); display.getSize(size); height = size.y; width = size.x; switch (rotation) { case Surface.ROTATION_90: if (width > height) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } break; case Surface.ROTATION_180: if (height > width) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } else { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } break; case Surface.ROTATION_270: if (width > height) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } else { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } break; default: if (height > width) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } } }