Check orientation on Android phone

前端 未结 23 1610
庸人自扰
庸人自扰 2020-11-22 06:26

How can I check if the Android phone is in Landscape or Portrait?

23条回答
  •  感情败类
    2020-11-22 07:18

    such this is overlay all phones such as oneplus3

    public static boolean isScreenOriatationPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }
    

    right code as follows:

    public static int getRotation(Context context) {
        final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
    
        if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
            return Configuration.ORIENTATION_PORTRAIT;
        }
    
        if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
            return Configuration.ORIENTATION_LANDSCAPE;
        }
    
        return -1;
    }
    

提交回复
热议问题