Tablet detection

Afternoon,

Is there an easy way to determine if a user is running an Android Tablet vs an Android Phone? I was thinking of using device aspect ratio to determine it.

Basically, I am trying to make my application run landscape-only on phones while on tablets allow them free range of orientation, so portrait and landscape.

Check display diagonal - tablets are for sure > 6 inches

not entirely sure how to check device diagonals, I know i *shouldn’t* use pixel length and width because smartphones can have a higher resolution then some tablets

You can read device height and width. Check api.

You can determine the display’s physical dimensions (in inches) via our system.getInfo() API…
   http://docs.coronalabs.com/api/library/system/getInfo.html
 
First, a word of warning.  Properties “androidDisplayWidthInInches” and “androidDisplayHeightInInches” have been known to return the wrong values on some devices.  We believe this to be a firmware bug where sometimes manufacturers have simply set these values wrong or forgot to set them.  It’s an issue that effects all native Android developers, making it impossible to tell what the physical screen sizes in inches reliably for all devices.
 
So, I recommend that you use the “androidDisplayApproximateDpi” property instead.  This returns a hard coded DPI value for devices designated as ldpi, mpdi, hdpi, xhdpi, etc.  It’s the only reliable means of fetching the DPI from all Android devices, with the only shortcoming is that it’s not accurate.  It is merely a rough estimate.  But this is as good as it gets on Android.
 
You can then calculate the approximate dimensions of the screen in inches as follows…

local approximateDpi = system.getInfo("androidDisplayApproximateDpi") local widthInInches = display.pixelWidth / approximateDpi local heightInInches = display.pixelHeight / approximateDpi

I hope this helps!

Wouldn’t the config file mess with the values for your device of the pixel width and pixel height?

Nope.  The width and height that you set in the “config.lua” file affects display.contentWidth and display.contentHeight.  The pixel width and height properties will remain the same.

Check display diagonal - tablets are for sure > 6 inches

not entirely sure how to check device diagonals, I know i *shouldn’t* use pixel length and width because smartphones can have a higher resolution then some tablets

You can read device height and width. Check api.

You can determine the display’s physical dimensions (in inches) via our system.getInfo() API…
   http://docs.coronalabs.com/api/library/system/getInfo.html
 
First, a word of warning.  Properties “androidDisplayWidthInInches” and “androidDisplayHeightInInches” have been known to return the wrong values on some devices.  We believe this to be a firmware bug where sometimes manufacturers have simply set these values wrong or forgot to set them.  It’s an issue that effects all native Android developers, making it impossible to tell what the physical screen sizes in inches reliably for all devices.
 
So, I recommend that you use the “androidDisplayApproximateDpi” property instead.  This returns a hard coded DPI value for devices designated as ldpi, mpdi, hdpi, xhdpi, etc.  It’s the only reliable means of fetching the DPI from all Android devices, with the only shortcoming is that it’s not accurate.  It is merely a rough estimate.  But this is as good as it gets on Android.
 
You can then calculate the approximate dimensions of the screen in inches as follows…

local approximateDpi = system.getInfo("androidDisplayApproximateDpi") local widthInInches = display.pixelWidth / approximateDpi local heightInInches = display.pixelHeight / approximateDpi

I hope this helps!

Wouldn’t the config file mess with the values for your device of the pixel width and pixel height?

Nope.  The width and height that you set in the “config.lua” file affects display.contentWidth and display.contentHeight.  The pixel width and height properties will remain the same.

This only works for android. Is there a way to do it both for android and ios?

Hi @gainsempire,

You won’t get those values returned on iOS. However, you can use other “tricks” to help determine if you’re using an iPad vs. an iPhone. See this tutorial for details:

http://coronalabs.com/blog/2012/12/11/device-detection-on-steroids/

Best regards,

Brent

This only works for android. Is there a way to do it both for android and ios?

Hi @gainsempire,

You won’t get those values returned on iOS. However, you can use other “tricks” to help determine if you’re using an iPad vs. an iPhone. See this tutorial for details:

http://coronalabs.com/blog/2012/12/11/device-detection-on-steroids/

Best regards,

Brent