How to detect a Phone or Tablet in Android

While developing on Android, we came across a rather simple problem:  How do I tell if my APK is installed on a Phone or a Tablet?

It seems like an easy one.  Surely there’s a “Tablet” flag, somewhere?  But then, what constitutes as a Tablet?
Is it the screen resolution?  We can’t really use this as, for example, in the Samsung Galaxy line of products, the Note 1 is a 1280×800 screen but so too is the Tab 8.9 – to name just two.

But here’s another interesting oddity: The normal rotation on a Phone is “Portrait”, whereas on a Tablet it’s “Landscape”.
That is, if you use getRotation() from the default display, then the “normal” rotation is when this returns a value of 0 (Zero).

The solution is actually very simple.
First, we get the rotation of the device.
If we have 0 or 180, then the device is in one of the “starting” positions.  That is, for a Phone it’s either the normal Portrait orientation, or it’s upside down, yet still portrait.  And similarly, for a Tablet, it’s in normal landscape or upside down landscape.
Now we know the device is in the “normal” or “start” position, we can check the display size.  This value changes depending on what orientation the device is in.  So, if we have a tablet that has a rotation of 0 (or 180), then it’s width will be greater than it’s height.  And of course, the opposite is true for a phone.

A quick check for if the devices rotation is 90 or 270, then we know it’s in the other orientation – Portrait for a Tablet, or Landscape for a Phone.
Now the height > width for a Tablet, so we know we have a tablet in this orientation.

A quick example.
First, get yourself a Display instance:

WindowManager windowManager = (WindowManager)context.getSystemService( Context.WINDOW_SERVICE );
Display display = windowManager.getDefaultDisplay();

Now we can check for the rotation and also get the screen size:

int rotation = display.getRotation();
Point size = new Point();
display.getSize( size );

And finally, perform a check on the rotation, followed by a check on the screen size, to determine what kind of hardware we are running, be it a Tablet or a Phone

if( Surface.ROTATION_0 == rotation || Surface.ROTATION_180 == rotation )
  if( size.x > size.y )
    // This is a Tablet and it is in Landscape orientation
    // This is a Phone and it is in Portrait orientation

The size instance we get from Display will change on every orientation change, so you’ll need to call getSize() on every orientation change.  Also, the size.x/y values related to the width and height of the screen.

Hope this helps someone!