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?
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.
When window. orientation returns 0 or 180 then you are in portrait mode, when returning 90 or 270 then you are in landscape mode.
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.
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
It's simple, Just a If-Else
block:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // landscape } else { // portrait }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With