Determine portrait mode or landscape mode

Description

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.  ja va2  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





















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS