Search code examples
androidscreen-orientationandroid-4.2-jelly-bean

android:screenOrientation="portrait" is ignored in Jellybean


android:screenOrientation="portrait" is ignored in Jellybean. If I turn the phone into landscape it will change orientation regardless. I have tried on several phones. Is there any new flag I have to use?


Solution

  • You can try to do it programmatically,

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        switch (newConfig.orientation)
        {
        case Configuration.ORIENTATION_PORTRAIT:
            // taking action on event
            lockScreenRotation(Configuration.ORIENTATION_PORTRAIT);
        break;
        case Configuration.ORIENTATION_LANDSCAPE:
            // taking action on event
            lockScreenRotation(Configuration.ORIENTATION_LANDSCAPE);
        break;
        case Configuration.ORIENTATION_SQUARE:
            // taking action on event
            lockScreenRotation(Configuration.ORIENTATION_SQUARE);
        break;
        default:
            throw new Exception("Unexpected orientation!!!");
        break;
     }
    
    
    
    
    private void lockScreenRotation(int orientation)
        {
          // Stop the screen orientation changing during an event
            switch (orientation)
            {
            ...
          case Configuration.ORIENTATION_PORTRAIT:
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
          case Configuration.ORIENTATION_LANDSCAPE:
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            break;
            ...
          case default:
           this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
           break;
            }
        }