Universal way to detect landscape mode in Android
Hello everybody,
Today I have faced a real challenge how to determinate the screen mode 🙂
Issue was to detect orientation (Landscape or portrait) for ANY Android device.
Xoom has 0,3 – landscape 1,2 – portrait, Galaxy 1,2 – landscape 0,3 – portrait,
so it is difficult to handle all necessary events for different devices due to theirs default orientation. I didn’t find the way to get default orientation, so I have implement a super easy way to detect if we are in Landscape mode:
1 |
<span style="color:#200080; font-weight:bold; ">String private mOrientation = "";</span> |
1 |
<span style="color:#200080; font-weight:bold; ">private</span> boolean isDeviceInTheLandscapeMode(Activity acitivity)<br /><span style="color:#406080; ">{</span><br /> <span style="color:#7779bb; ">int</span> width <span style="color:#308080; ">=<br /></span> acitivity<span style="color:#308080; ">.</span>getWindowManager<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span><span style="color:#308080; ">.</span>getDefaultDisplay<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span><span style="color:#308080; ">.</span>getWidth<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span><br /> <span style="color:#7779bb; ">int</span> height <span style="color:#308080; ">=<br /></span> acitivity<span style="color:#308080; ">.</span>getWindowManager<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span><span style="color:#308080; ">.</span>getDefaultDisplay<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span><span style="color:#308080; ">.</span>getHeight<span style="color:#308080; ">(</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span><br /><br /> <span style="color:#7779bb; ">boolean</span> isLandscape <span style="color:#308080; ">=</span> width <span style="color:#308080; ">></span> height<span style="color:#406080; ">;</span><br /><br /> <span style="color:#200080; font-weight:bold; ">if</span><span style="color:#308080; ">(</span>isLandscape<span style="color:#308080; ">)</span><br /> mOrientation <span style="color:#308080; ">=</span> <span style="color:#1060b6; ">"(>.>)"</span><span style="color:#406080; ">;</span><br /> <span style="color:#200080; font-weight:bold; ">else</span><br /> mOrientation <span style="color:#308080; ">=</span> <span style="color:#1060b6; ">"(^.^)"</span><span style="color:#406080; ">;</span><br /><br /> <span style="color:#200080; font-weight:bold; ">return</span> isLandscape<span style="color:#406080; ">;</span> <span style="color:#595979; ">// landscape is true</span><br /><span style="color:#406080; ">}</span><br /> |
Notes:
To retrieve current orientation id, you are able to use:
1 |
activity.getWindowManager().getDefaultDisplay().getOrientation()<br /> |
This function returns 0,1,2 or 3 integers, which are corresponds to the appropriate constants in Surface class:
1 |
Surface.ROTATION_90<br />Surface.ROTATION_0<br />Surface.ROTATION_180<br />Surface.ROTATION_270<br /> |
Happy orientation detection ^__^
Best regards,
Yahor