SamRowley SamRowley - 1 year ago 154
Android Question

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?

Answer Source

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'd 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 numuber), see the Configuration class API documentation

if (orientation==Configuration.ORIENTATION_LANDSCAPE) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download