Reputation: 3147
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;
}
}
Upvotes: 4
Views: 1811
Reputation: 5288
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().
Upvotes: 1
Reputation: 6755
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
Upvotes: 0
Reputation: 3323
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 =)
Upvotes: 0
Reputation: 4676
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!
Upvotes: 2