4

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;
                }   
            }

4 Answers 4

2

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!

2
  • I was hoping for a method I overlooked in the SDK. I don't think going down the route of checking Device/Model/Product descriptions is a good idea. However, checking for xlarge/large might be better. Im sure xlarge will cover all 7"+ tablets, however think large might end up affecting galaxy nexus, Samsung note and Samsung S2 and S3. Thanks for the idea, ill give it a go
    – JonWillis
    May 20, 2012 at 13:24
  • Configuration.SCREENLAYOUT_SIZE_LARGE and 4 (as Configuration.SCREENLAYOUT_SIZE_XLARGE is API9), worked a charm. At least on my physical test devices here.
    – JonWillis
    May 20, 2012 at 22:48
1

There is another approach. You can use a combination of the current Configuration's orientation and the Display rotation to determine the natural orientation of the device, as described here: How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout). With the getDeviceDefaultOrientation() method described there, you could set your isTablet variable as follows:

isTablet = (getDeviceDefaultOrientation() == Configuration.ORIENTATION_LANDSCAPE)

If your app sometimes forces configuration into portrait or landscape mode, you may want to call getDeviceDefaultOrientation() early in the life of the app (prior to forcing config orientation) and store the value for later use. Otherwise, you could be in a state where the device's physical rotation is out of synch with the config orientation, as often happens when a video is forced into landscape mode for full-screen viewing while the device itself still has a portrait rotation. This can be accomplished by calling Activity.setRequestedOrientation() at runtime, AFTER storing the result of getDeviceDefaultOrientation().

0

When the device orientation changes, your activity is destroyed and recreated, meaning that you don't need to actually "listen" to a specific event. To know if you are in portrait or landscape orientation, no matter what kind of device is running your code :

switch (yourContext.getResources().getConfiguration().orientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        //xxx
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        //xxx
        break;
}

"onCreate" could be a good location for that =)

1
  • The activity is not being destroyed. As I am using the camera on a surface view, I do not change from portrait to landscape. Thus onCreate is not called each time. Instead, I need to listen to the orientation sensor and reposition manually. Technically I never leave portrait orientation.
    – JonWillis
    May 20, 2012 at 13:21
0

Actually, I've used an awful hack to solve the same problem (and I am genuinely ashamed :). When you get your sensor orientation, compare it with screen size ratio (wide vs. tall).

ORIENTATION FROM SENSOR == ORIENTATION FROM SCREEN means PHONE, otherwise TABLET (or TV, ... or whatever's coming). Here's the code I use to get the screen ratio/orientation (unfortunately only 2 modes are recognizable).

int getScreenOrientation(Activity ctx)  {
  Point pt = new Point(0,0);
  try {
    Display dsp =  ctx.getWindowManager().getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
      dsp.getSize(pt);
    } else {
      pt.x = dsp.getWidth();   
      pt.y = dsp.getHeight();
    }
  return  (pt.x <= pt.y) ?  
    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT :
    ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
  }
  catch (Exception e) {}
  return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}

This solves only the orientation problem, it does not detect if it is a tablet. As a matter fact, most of my 7inch tabs do not have orientation mismatch issue. I found it only on my NEXUS 10 and a TV android dongle.

"Don't code like me and you'll be fine."

sean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.