Wednesday, June 27, 2012

How to find Android default screen orientation.


Recently while developing my personal project, I had a requirement to find the default screen orientation of device. Tablet default screen orientation is landscape whereas Mobile phone default screen orientation is portrait. 
Well after a little bit of research I found that getRotation method can fetch me the default orientation. This method can be used in the following way: 
this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()
               .getRotation()
 If this method returns Surface.ROTATION_0 (no rotation) then it means that the current screen orienation is the default screen orientation.  For example, if the tablet is currently in  landscape orientation then this method returns Surface.ROTATION_0(value 0x00000000) else it returns either  Surface.ROTATION_90 or Surface.ROTATION_270.
This arises one more question as how to determine the current orientation of the screen. It can be determined by the following method.
this.getResources().getConfiguration().orientation
The return value can be either of these
Configuration.ORIENTATION_LANDSCAPE
Configuration.ORIENTATION_PORTRAIT
Configuration.ORIENTATION_SQUARE
These values themselves suggest the orientation of the screen. 

Your valuable comments are always welcomed. It will help to improve my post and understanding.

"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."
By : Confucius