Check orientation on Android phone

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

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

23条回答
  •  醉酒成梦
    2020-11-22 07:17

    Some time has passed since most of these answers have been posted and some use now deprecated methods and constants.

    I've updated Jarek's code to not use these methods and constants anymore:

    protected int getScreenOrientation()
    {
        Display getOrient = getWindowManager().getDefaultDisplay();
        Point size = new Point();
    
        getOrient.getSize(size);
    
        int orientation;
        if (size.x < size.y)
        {
            orientation = Configuration.ORIENTATION_PORTRAIT;
        }
        else
        {
            orientation = Configuration.ORIENTATION_LANDSCAPE;
        }
        return orientation;
    }
    

    Note that the mode Configuration.ORIENTATION_SQUARE isn't supported anymore.

    I found this to be reliable on all devices I've tested it on in contrast to the method suggesting the usage of getResources().getConfiguration().orientation

提交回复
热议问题