Check orientation on Android phone

前端 未结 23 1599
庸人自扰
庸人自扰 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;
    }
    
    0 讨论(0)
  • 2020-11-22 07:18

    i think using getRotationv() doesn't help because http://developer.android.com/reference/android/view/Display.html#getRotation%28%29 getRotation() Returns the rotation of the screen from its "natural" orientation.

    so unless you know the "natural" orientation, rotation is meaningless.

    i found an easier way,

      Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);
      int width = size.x;
      int height = size.y;
      if(width>height)
        // its landscape
    

    please tell me if there is a problem with this someone?

    0 讨论(0)
  • 2020-11-22 07:18

    Just simple two line code

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // do something in landscape
    } else {
        //do in potrait
    }
    
    0 讨论(0)
  • 2020-11-22 07:19

    Another way of solving this problem is by not relying on the correct return value from the display but relying on the Android resources resolving.

    Create the file layouts.xml in the folders res/values-land and res/values-port with the following content:

    res/values-land/layouts.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="is_landscape">true</bool>
    </resources>
    

    res/values-port/layouts.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="is_landscape">false</bool>
    </resources>
    

    In your source code you can now access the current orientation as follows:

    context.getResources().getBoolean(R.bool.is_landscape)
    
    0 讨论(0)
  • 2020-11-22 07:20

    A fully way to specify the current orientation of the phone:

    public String getRotation(Context context) {
        final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
        switch (rotation) {
            case Surface.ROTATION_0:
                return "portrait";
            case Surface.ROTATION_90:
                return "landscape";
            case Surface.ROTATION_180:
                return "reverse portrait";
            default:
                return "reverse landscape";
        }
    }
    
    0 讨论(0)
提交回复
热议问题