Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect landscape left (normal) vs landscape right (reverse) with support for naturally landscape devices?

What I would like to do:

  • Detect the current layout orientation of the device (Portrait, Landscape-Left, Portrait(upside down), Landscape-Right)
  • Differentiate between the two possible landscape modes (leaning on the left side, leaning on the right side)
  • Support devices that have landscape as their natural state (for example, discussed here)
  • Support < 2.2 (can't use getRotation())

Just to clarify, I want to know what orientation the layout is currently in. Which direction is the bottom of the layout? Is it in portrait? Is it in landscape? Is the landscape left or right? etc.

The closest I can find is Activity.getResources().getConfiguration().orientation, but it only returns three possible values. ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, or ORIENTATION_SQUARE. It does not say which type of landscape.

So, then I thought, well if I can get the orientation value (like 90, 180, 270, etc) of the device and compare it to the above config value I could figure it out. For example, if the config value was ORIENTATION_LANDSCAPE and the orientation was 270, I could tell that it was in the reverse landscape mode. However, there doesn't appear to be an easy way to get the orientation value. I could implement an OrientationEventListener, but that seems overkill since I just need to get the value once at a specific time, not constantly.

There are two values in the Display class but getOrientation is deprecated and getRotation is only available for 2.2 and above.

Now, even if I did get this value, there is still the issue of devices that are naturally landscape. Meaning their 0 value would be landscape instead of portrait. So I also need a sure way to tell which (landscape-left, landscape-right or portrait) corresponds to 0.

There are a lot of posts on SO about orientation and similar issues, but I haven't seen any that take all of these issues into consideration. Has anyone figured this out?

When all is said and done I would like something like Activity.getResources().getConfiguration().orientation but be able to tell which landscape mode it is.

like image 239
cottonBallPaws Avatar asked Feb 23 '11 09:02

cottonBallPaws


1 Answers

Right landscape wasn't supported till 2.2. You'll need to have <2.2 devices choose in the options or something.

like image 186
AaronM Avatar answered Oct 19 '22 13:10

AaronM