get Device Default Orientation - Android Hardware

Android examples for Hardware:Device Feature

Description

get Device Default Orientation

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.res.Configuration;
import android.view.Surface;
import android.view.WindowManager;

public class Main {
    public static int getDeviceDefaultOrientation(Context context) {
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Configuration config = context.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 {//from w w w  . j  av a  2  s .  c o  m
            return Configuration.ORIENTATION_PORTRAIT;
        }
    }
}

Related Tutorials