Find out if Android device is portrait or landscape for normal usage?

Viewed 47931

Is there anyway to find out if a device is portrait or landscape by default? In that I mean how you normally use the device.

Most phones have a portrait screen for normal usage but is there some flag for finding that out?

8 Answers

It's simple, Just a If-Else block:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    // landscape
} else {
    // portrait
}
// Current orientation
public boolean landscape = false;

public boolean isLandscape(){

    DisplayMetrics displaymetrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int height = displaymetrics.heightPixels;

    if(width<height){
        landscape = false;
    }
    else{
        landscape = true;
    }

    return landscape;

}
Related