Android How to - Determine portrait mode or landscape mode








You can use the WindowManager to determine whether the device is currently in portrait mode or landscape mode.

Example

package com.java2s.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
// w  w  w  . java2 s . com
public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //get the current display info
        WindowManager wm = getWindowManager();
        Display d = wm.getDefaultDisplay();
        if (d.getWidth() > d.getHeight())
        {
            //landscape mode
        }else{
            //portrait mode
        }
    }
}




Note

The code above check the orientation in the onCreate method. The logic is only called when the onCreate method is invoked