0

How to convert onOrientationChanged methods orientation value to landscape and portrait to know the device orientation.

I have the following function that gets called every time when device orientations changes.
public void onOrientationChanged(int orientation)

the parameter orientation is in between range 0 to 359.
So how can I get to know from these values if the device is in landscape mode or portrait mode?

Swati Garg
  • 995
  • 1
  • 10
  • 21
User7723337
  • 11,857
  • 27
  • 101
  • 182
  • possible duplicate of [Check orientation on Android phone](http://stackoverflow.com/questions/2795833/check-orientation-on-android-phone) – devnull Aug 06 '13 at 07:48

2 Answers2

1

you can get orientation by using this

if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
{
  // do your stuff          
}
else
{
   // do your stuff         
}
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • I can't use onConfig change method as my application is using set orientation. So i am using onOrientationChanged method. – User7723337 Aug 06 '13 at 08:09
0

You can get the orientation in runtime with this code:

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
//orientation will contain Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180 or Surface.ROTATION_270
int orientation = display.getRotation();

So, if you want to make something when changing the orientation you can override the onConfigurationChanged method.

And if you want to change the orientation in runtime you can use this (in example, change to portrait):

setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

If want more details, read this

Renan Bandeira
  • 3,238
  • 17
  • 27