Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if Android device is portrait or landscape for normal usage?

Is there anyway to find out if a device is portrait or landscape by default? In that I mean how you normally use the device.

Most phones have a portrait screen for normal usage but is there some flag for finding that out?

like image 886
joynes Avatar asked Sep 09 '10 08:09

joynes


People also ask

How do I find the orientation of my Android phone?

If the activity locks the display ( android:screenOrientation="portrait" ), this method will return the same value irrespective of how the user rotates the device. In that case you'd use the accelerometer or the gravity sensor to figure out orientation properly.

How do I know if my screen is portrait or landscape?

When window. orientation returns 0 or 180 then you are in portrait mode, when returning 90 or 270 then you are in landscape mode.

How do I manage portrait and landscape on Android?

Rotate your phone to change the screen orientation (if Auto Rotate is enabled). If Auto Rotate is enabled, your phone's screen will automatically flip to portrait mode when you are holding it upright. When you are holding it horizontally, it will automatically switch to Landscape mode.


2 Answers

You can do this by:

For Lanscape

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {     // Do some stuff } 

For Portrait

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {     // Do some stuff } 

Check: Configuration.orientation

like image 82
Hazem Farahat Avatar answered Oct 06 '22 00:10

Hazem Farahat


It's simple, Just a If-Else block:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {     // landscape } else {     // portrait } 
like image 24
Sheikh Hasib Avatar answered Oct 05 '22 23:10

Sheikh Hasib