Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Screen rotation using Display.getRotation()

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?

like image 742
SamRowley Avatar asked Sep 17 '25 22:09

SamRowley


1 Answers

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) 
like image 50
Vincent Mimoun-Prat Avatar answered Sep 19 '25 11:09

Vincent Mimoun-Prat