Android examples for Phone:Screen
set Screen Orientation Fixed
/*/*w ww. j a v a2 s. co m*/ * Copyright (C) 2012 Simon Robinson * * This file is part of Com-Me. * * Com-Me is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 3 of the * License, or (at your option) any later version. * * Com-Me is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Com-Me. * If not, see <http://www.gnu.org/licenses/>. */ //package com.java2s; import android.annotation.TargetApi; 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; import android.view.WindowManager; public class Main { @TargetApi(Build.VERSION_CODES.GINGERBREAD) public static void setScreenOrientationFixed(Activity activity, boolean orientationFixed) { if (orientationFixed) { WindowManager windowManager = activity.getWindowManager(); boolean naturallyPortrait = getNaturalScreenOrientation(windowManager) == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; int reversePortrait = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; int reverseLandscape = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { reversePortrait = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; reverseLandscape = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } switch (windowManager.getDefaultDisplay().getRotation()) { case Surface.ROTATION_0: activity.setRequestedOrientation(naturallyPortrait ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; case Surface.ROTATION_90: activity.setRequestedOrientation(naturallyPortrait ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : reversePortrait); break; case Surface.ROTATION_180: activity.setRequestedOrientation(naturallyPortrait ? reversePortrait : reverseLandscape); break; case Surface.ROTATION_270: activity.setRequestedOrientation(naturallyPortrait ? reverseLandscape : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; } } else { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); } } /** * Get the "natural" screen orientation - i.e. the orientation in which this device is designed to be used most * often. * * @param windowManager * @return either ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE or ActivityInfo.SCREEN_ORIENTATION_PORTRAIT */ public static int getNaturalScreenOrientation( WindowManager windowManager) { Display display = windowManager.getDefaultDisplay(); Point screenSize = getScreenSize(windowManager); int width = 0; int height = 0; switch (display.getRotation()) { case Surface.ROTATION_0: case Surface.ROTATION_180: width = screenSize.x; height = screenSize.y; break; case Surface.ROTATION_90: case Surface.ROTATION_270: width = screenSize.y; height = screenSize.x; break; default: break; } if (width > height) { return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } @SuppressWarnings("deprecation") public static Point getScreenSize(WindowManager windowManager) { Point screenSize = new Point(); Display display = windowManager.getDefaultDisplay(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { getPointScreenSize(display, screenSize); } else { screenSize.set(display.getWidth(), display.getHeight()); } return screenSize; } @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) private static void getPointScreenSize(Display display, Point screenSize) { display.getSize(screenSize); } }