Screen rotation using Display.getRotation()

13,736

Solution 1

As far as I know there is no support for inverted layout before 2.3. So unless you are drawing the screen with a custom SurfaceView I'd say you cannot do it using standard widgets. With a surface view, you would simply need to transform the whole canvas before rendering to it.

Additionally, you should use constants in order to know the current orientation (no magic numbers), see the Configuration class API documentation

if (orientation==Configuration.ORIENTATION_LANDSCAPE) 

Solution 2

getRotation for class 'Display' is explained at https://developer.android.com/reference/android/view/Display.html#getRotation()

getRotation returns:

Surface.ROTATION_0 (no rotation),
Surface.ROTATION_90,
Surface.ROTATION_180,
or Surface.ROTATION_270.

For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

Share:
13,736
SamRowley
Author by

SamRowley

iOS developer at Jaguar Land Rover. Been developing for iOS since 2010

Updated on June 04, 2022

Comments

  • SamRowley
    SamRowley almost 2 years

    I am trying to make it so my app never goes into portrait mode but is able to switch between the 2 landscape screen views. I know this can be done easily in Gingerbread (2.3) but I am having trouble doing it manually for the other versions of android, my code is as follows:

    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
            int orientation = display.getRotation();
            if (orientation == 1) {
                /* The device is rotated to the left. */
                Log.v("Left", "Rotated Left");
            } else if (orientation == 3) {
                /* The device is rotated to the right. */
            Log.v("Right", "Rotated Right");
            } else { 
    
            }
    

    My problem is how do I flip the x and y-axis of the screen view depending on the rotation detected? How do I get hold of them in order to reverse them?

  • SamRowley
    SamRowley over 13 years
    There must be a way to set the x and y-axis manually? how does the android OS know to do it on it's own?
  • Vincent Mimoun-Prat
    Vincent Mimoun-Prat over 13 years
    AFAIK not possible, see mail-archive.com/[email protected]/… (a permission would be needed but is not available in the SDK)
  • SamRowley
    SamRowley over 13 years
    is it possible to return a similar value as the getRotation() method but not in the context of a display, so according to the accelerometer?
  • Vincent Mimoun-Prat
    Vincent Mimoun-Prat over 13 years
    Yeah you can poll yourself any sensor you want and deduce the current orientation of the phone: blog.androgames.net/135/android-orientation-tutorial