Recently i was searching a way to know screen size and orientation for "orient_undefined" in android at run time as i wanted to do different operation in landscape and portrait mode though i searched on the net all i got was bits here is the code which works on all the devices and screen resolution
public int getscrOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = getOrient.getOrientation();
// Sometimes you may get undefined orientation Value is 0
// simple logic solves the problem compare the screen
// X,Y Co-ordinates and determine the Orientation in such cases
if(orientation==Configuration.ORIENTATION_UNDEFINED){
Configuration config = getResources().getConfiguration();
orientation = config.orientation;
if(orientation==Configuration.ORIENTATION_UNDEFINED){
//if height and widht of screen are equal then
// it is square orientation
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
}else{ //if widht is less than height than it is portrait
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else{ // if it is not any of the above it will defineitly be landscape
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
}
}
return orientation; // return value 1 is portrait and 2 is Landscape Mode
}
if there is any other alternative way to get the value of undefined orientation do share with us and let us know
13 comments:
Thank you for usefull tip. Used it in upcoming game.
Denis A Gladkiy.
Piggybank Software.
On HTC hero there is one issue: orientation retrieved from display (1: portrait) differs from orientation retrieved from configuration (2: landscape). Actually there was landscape orientation.
hi masterden thanx for leaving comment... actually i am using this code on htc hero, and g1 it is working fine lemme double check it....
Hi
I am maybe a little late compared to the date of this post, but a OrientationEventListener exists in android. It is probably easier to use it.
Thanks for this example.
Samsung Galaxy S has two landscape modes (clockwise and counter-clockwise), this method returns orientation 3 (ORIENTATION_SQUARE) when in counter-clockwise-landscape mode.
I've found out that reversing the checking order works - first check
getResources().getConfiguration().orientation
and only if it doesn't say anything useful, check
getOrient.getOrientation()
Hope this helps,
MD
not true. On droid i always get result 1
This method is deprecated, use:
getRotation()
Hi
I have tried your approach and always I endup getting 1 as a result on Nexus S - 2.3 OS. What is alternate solution for this?
Best regards
Sudhakar chavali
use "getRotation()"
it was used when there was no APIs to get the screen orientation now the method described in this website is not required "Getorientation" directly gives the orientation
It is more simple that that and the above technique does not work:
Use:
int currOrientation = getResources().getConfiguration().orientation;
You can use this it returns
0-portrait
1-Landscape
/* First, get the Display from the WindowManager */
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
/* Now we can retrieve all display-related infos */
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();
display.getOrientation() is now replaced with
display.getRotation()
Post a Comment