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

3 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....
Post a Comment