Created
          September 11, 2017 20:06 
        
      - 
      
- 
        Save javier-delgado/00d2019d37ca4700a77cb737e13e3900 to your computer and use it in GitHub Desktop. 
    Utilidad para bloquear programaticamente la rotacion de pantalla
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public class ScreenLock { | |
| public static class Orientation { | |
| public static final int LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; | |
| public static final int PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; | |
| public static final int REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; | |
| public static final int REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; | |
| public static final int UNSPECIFIED = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; | |
| } | |
| @SuppressLint("NewApi") | |
| @SuppressWarnings("deprecation") | |
| public static void lockOrientation(final Activity activity) { | |
| final Display display = activity.getWindowManager().getDefaultDisplay(); | |
| final int rotation = display.getRotation(); | |
| final int width, height; | |
| if (Build.VERSION.SDK_INT >= 13) { | |
| Point size = new Point(); | |
| display.getSize(size); | |
| width = size.x; | |
| height = size.y; | |
| } | |
| else { | |
| width = display.getWidth(); | |
| height = display.getHeight(); | |
| } | |
| switch (rotation) { | |
| case Surface.ROTATION_90: | |
| if (width > height) { | |
| activity.setRequestedOrientation(Orientation.LANDSCAPE); | |
| } | |
| else { | |
| activity.setRequestedOrientation(Orientation.REVERSE_PORTRAIT); | |
| } | |
| break; | |
| case Surface.ROTATION_180: | |
| if (height > width) { | |
| activity.setRequestedOrientation(Orientation.REVERSE_PORTRAIT); | |
| } | |
| else { | |
| activity.setRequestedOrientation(Orientation.REVERSE_LANDSCAPE); | |
| } | |
| break; | |
| case Surface.ROTATION_270: | |
| if (width > height) { | |
| activity.setRequestedOrientation(Orientation.REVERSE_LANDSCAPE); | |
| } | |
| else { | |
| activity.setRequestedOrientation(Orientation.PORTRAIT); | |
| } | |
| break; | |
| default: | |
| if (height > width) { | |
| activity.setRequestedOrientation(Orientation.PORTRAIT); | |
| } | |
| else { | |
| activity.setRequestedOrientation(Orientation.LANDSCAPE); | |
| } | |
| } | |
| } | |
| public static void unlockOrientation(final Activity activity) { | |
| activity.setRequestedOrientation(Orientation.UNSPECIFIED); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment