Search code examples
androidmobilescreenorientationtablet

OrientationEventListener (Tablet vs Mobile) 90 degree difference


Im using OrientationEventListener to detect the orientation, but I have a problem in that the tablets are landscape and phones are portrait by default. This means that OrientationEventListener returns a value of 90 for portrait on tablets, but 0 for portrait on mobiles.

The activity I am using has the camera so I cannot change between orientations, thus I use the value of Orientation to reposition a couple of elements on the screen as needed.

Is it possible to detect if the device is a tablet, so that I can adjust the value accordingly. i.e. How do I work out the value of isTablet?

            if(isTablet)
            {
                orientation += -90;
                if(orientation < 0) //Check if we have gone too far back, keep the result between 0-360
                {
                    orientation += 360;
                }   
            }

Solution

  • Since you have mentioned that you cannot change between orientations, you'll have to use the device's properties to figure out if it is a tablet.

    Take a look at this reference.

    You can use android.os.Build.DEVICE, android.os.Build.MODEL and android.os.Build.PRODUCT to get the identity of the device, and from this knowledge, you can use this reference to find their values and determine what is the device type.

    But using this method, you'd have to update the software each time a new tablet is released. (I have used this question as a reference for this part of the answer).

    Alternate is what I have found here, quoting from the answer:

    public boolean isTablet(Context context) {
        boolean xlarge = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
        boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        return (xlarge || large);
    }
    

    LARGE and XLARGE Screen Sizes are determined by the manufacturer based on the distance from the eye they are to be used at (thus the idea of a tablet).

    Hope this helps!