How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout)

This method can help:--

public int getDeviceDefaultOrientation() {

    WindowManager windowManager =  (WindowManager) getSystemService(Context.WINDOW_SERVICE);

    Configuration config = getResources().getConfiguration();

    int rotation = windowManager.getDefaultDisplay().getRotation();

    if ( ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
            config.orientation == Configuration.ORIENTATION_LANDSCAPE)
        || ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&    
            config.orientation == Configuration.ORIENTATION_PORTRAIT)) {
      return Configuration.ORIENTATION_LANDSCAPE;
    } else { 
      return Configuration.ORIENTATION_PORTRAIT;
    }
}

Well, you can find out what current orientation is the way @Urboss said. You cannot get the layout (landscape/portrait) that way ofcourse, so you'll have to get the screen width/heigth to check if the current (be it changed or not, but you've checked that ;) ) position is landscape or portrait:

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    wPix = dm.widthPixels;
    hPix = dm.heightPixels;

(so if the rotation is 0 and you get landscape with above metrix, your device is default landscape. If the rotation is 90 degrees and you're portrait, the default is also landscape, and so on)


After hours and hours of trying to figure this out. It's not possible. However, the closest thing you can do is to set the orientation to "NOSENSOR"

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

What this will do is set your application to the natural orientation of the device. At this point you can get the height and width of the display using the DisplayMetrics class, and calculate if you are in landscape or portrait. After you then figure out if it's landscape or portrait, you can then do this

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_XXXXX);

Where XXXX is either LANDSCAPE or PORTRAIT.

The case where doing this may not work is if you have a slide out keyboard.


Thanks to diyism's excellent answer above, I also added the logic to check the actual orientation position (landscape right, portrait, landscape left, portrait flipped): enter image description here Here is the code:

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
    //The coordinate-system is defined relative to the screen of the phone in its default orientation
    int orientation = 0;
    float roll=0;
    float pitch=0;
    switch (getWindowManager().getDefaultDisplay().getRotation())  {
        case Surface.ROTATION_0:
                 roll=event.values[2];
                 pitch=event.values[1];
            break;
            case Surface.ROTATION_90:
                 roll=event.values[1];
                 pitch=-event.values[2];
            break;
            case Surface.ROTATION_180:
                 roll=-event.values[2];
                 pitch=-event.values[1];
            break;
            case Surface.ROTATION_270:
                 roll=-event.values[1];
                 pitch=event.values[2];
            break;
           }

    if (pitch >= -45 && pitch < 45 && roll >= 45)
        orientation = 0;
    else if (pitch < -45 && roll >= -45 && roll < 45) 
        orientation = 1; 
    else if (pitch >= -45 && pitch < 45 && roll < -45) 
        orientation = 2;
    else if (pitch >= 45 && roll >= -45 && roll < 45 ) 
        orientation = 3;

    if (m_nOrientation != orientation) { //orientation changed event
        m_Inst.Debug(LOG_TAG,"onSensorChanged: orientation:" + orientation);
        m_nOrientation = orientation;
        // fire event for new notification, or update your interface here
    }
}

This code is also posted here: http://www.pocketmagic.net/?p=2847


Here's my solution:

public class ActivityOrientationTest extends Activity {
private static final String TAG = "ActivityOrientationTest";
private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
private TextView mTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.main);
    mTextView = (TextView)findViewById(R.id.text);
    setDefaultOrientation();        
}

private void setDefaultOrientation(){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);

    Display display;         
    display = getWindow().getWindowManager().getDefaultDisplay();
    int rotation = display.getRotation();
    int width = 0;
    int height = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
    case Surface.ROTATION_180:
        Log.i(TAG, "Rotation is: 0 or 180");
        width = display.getWidth();
        height = display.getHeight();
        break;
    case Surface.ROTATION_90:       
    case Surface.ROTATION_270:
        Log.i(TAG, "Rotation is: 90 or 270");
        width = display.getHeight();
        height = display.getWidth();
        break;
    default:
        break;
    }

    if(width > height){
        Log.i(TAG, "Natural Orientation is landscape");
        mTextView.setText("Natural Orientation is landscape");
        mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    } else {
        Log.i(TAG, "Natural Orientation is portrait");
        mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        mTextView.setText("Natural Orientation is portrait");
    } 

    setRequestedOrientation(mNaturalOrientation);
}

Display.getRotation() is only supported in API level 8 or higher so if you need to support older devices you'll need to call Display.getOrientation().