How do detect Android Tablets in general. Useragent?
Asked Answered
S

15

108

I have looked everywhere. We are using a Motorola Zoom to try our tablet site testing. The issue is that the Android Useragent is a general Useragent and there is no difference between tablet Android and mobile Android. I don't want to just target a specific device like the Xoom Useragent since Android will most likely be on multiple tablet devices in the near future.

We are using Umbraco CMS, and we have looked at using the 51Degrees.mobi solution, and at this moment this will not work for our needs. Maybe in the future. I know 51Degrees and some of the people at Umbraco are going to be doing some integration into Umbraco, but the final project will probably not be out for a couple of months.
Hence, why we would like to detect the Useragent String of an Android tablet and have it not direct to our mobile site like it currently is.

If anyone knows how to detect and Android Tablet in general, not the specific device would be very helpful.

Shewmaker answered 17/3, 2011 at 15:56 Comment(3)
I'm just guessing, but perhaps there's a way to see how high the viewing resolution is as well?Remex
@Phonon, Yes we are trying to use Javascript to send a res value back to the server. We are trying to us a __doPostBack but that seems to not be sending our value back. We have a hidden input value, it shows client side but we are not getting it back server side.Shewmaker
Not sure if I can help you here.Remex
E
115

The issue is that the Android User-Agent is a general User-Agent and there is no difference between tablet Android and mobile Android.

This is incorrect. Mobile Android has "Mobile" string in the User-Agent header. Tablet Android does not.

But it is worth mentioning that there are quite a few tablets that report "Mobile" Safari in the userAgent and the latter is not the only/solid way to differentiate between Mobile and Tablet.

Eohippus answered 17/3, 2011 at 19:48 Comment(10)
you are correct. I was just looking for a string that contains Android. I had to search for the string that contains Android and !mobile. Found the article by Tim Bray about this.Shewmaker
Wurfl still has 'mobile' as part of Xoom's user agent: tera-wurfl.com/explore/…Emersion
@yoavf: That appears to have been fixed with the XOOM's Android 3.1 update. I own a XOOM and just tested it -- no "mobile".Eohippus
This answer is not completely correct. There are quite a few tablets - even the newest Kindle Fire - that report "Mobile" Safari in the userAgent string. There doesn't seem to be a solid way of detecting tablet vs. phone at this point unless it's a combination of userAgent and possible support for resolution detection.Orthoepy
Also this is worth looking at: developers.google.com/chrome/mobile/docs/user-agentOniskey
false! samsung galaxy tab is obvious tablet but it has "mobile" in its user agentEstis
I believe the "mobile" addition was with Android 4, there's still a lot of devices pre-4... For android 2.x I assume phone, for 3.x I assume tablet (though there will be a few false positives)... otherwise you can test for mobile...Contributory
I had the same issue, the android tablet was being directed to mobile phone websites instead full websites. Most websites were treating android tablet devices like android phone and were displaying the mobile website instead full website. I had a hack, though I am not satisfied with this hack but it does my job and getting expected result. I had to use iPad agent like "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25" . If someone has better solution please provide.Drewdrewett
@Eohippus see in galaxy tab 2 , i have tried use the useragent but the user-Agent header has a Mobile string in the tablet itselfConfer
thank you. Without you I wouldn't have notice this.Stroller
H
20

@Carlos: In his article Tim Bray recommends this (as does another post by Google), but unfortunately it is not being applied by all tablet manufacturers.

... We recommend that manufactures of large-form-factor devices remove "Mobile" from the User Agent...

Most Android tablet user-agent strings I've seen use mobile safari, e.g. the Samsung Galaxy Tab:

Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

So at the moment I am checking on device names to detect Android tablets. As long as there are just a few models on the market, that's ok but soon this will be an ugly solution.

At least in case of the XOOM, the mobile part seems to be gone:

Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

But as there are currently only tablets with Andorid 3.x, checking on Android 3 would be enough.

Hypoderm answered 8/4, 2011 at 15:3 Comment(1)
could you post the list of devices you checked against and the coding.Naval
D
12

Mo’ better to also detect “mobile” user-agent

While you may still want to detect “android” in the User-Agent to implement Android-specific features, such as touch-screen optimizations, our main message is: Should your mobile site depends on UA sniffing, please detect the strings “mobile” and “android,” rather than just “android,” in the User-Agent. This helps properly serve both your mobile and tablet visitors.

Detecting Android device via Browser

 < script language="javascript"> <!--
     var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
              if (mobile) {
                  alert("MOBILE DEVICE DETECTED");
                  document.write("<b>----------------------------------------<br>")
                  document.write("<b>" + navigator.userAgent + "<br>")
                  document.write("<b>----------------------------------------<br>")
                  var userAgent = navigator.userAgent.toLowerCase();
                  if ((userAgent.search("android") > -1) && (userAgent.search("mobile") > -1))
                         document.write("<b> ANDROID MOBILE <br>")
                   else if ((userAgent.search("android") > -1) && !(userAgent.search("mobile") > -1))
                       document.write("<b> ANDROID TABLET <br>")
              }
              else
                  alert("NO MOBILE DEVICE DETECTED"); //--> </script>
Drive answered 14/10, 2011 at 5:6 Comment(2)
This works perfectly. I added onto this for iPad and iPhone/iPod. else if (userAgent.search("ipad") > -1) document.write("<b> iPad <br>") else if ((userAgent.search("iphone") > -1)||(userAgent.search("ipod") > -1)) document.write("<b> iPhone or iPod <br>")Moyra
How does it work for mozilla/5.0 (linux; u; android 2.2; en-gb; gt-p1000 build/froyo) applewebkit/533.1 (khtml, like gecko) version/4.0 mobile safari/533.1 which has the word 'mobile' in it for the Samsung Galaxy Tablet?Naval
I
8

You can try this script out since you do not want to target the Xoom only. I don't have a Xoom, but should work.

function mobile_detect(mobile,tablet,mobile_redirect,tablet_redirect,debug) {
var ismobile = (/iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(navigator.userAgent.toLowerCase()));
var istablet = (/ipad|android|android 3.0|xoom|sch-i800|playbook|tablet|kindle/i.test(navigator.userAgent.toLowerCase()));

if (debug == true) {
    alert(navigator.userAgent);
}

if (ismobile && mobile==true) {
    if (debug == true) {
        alert("Mobile Browser");
    }
    window.location = mobile_redirect;
} else if (istablet && tablet==true) {
    if (debug == true) {
        alert("Tablet Browser");
    }
    window.location = tablet_redirect;
}
}

I created a project on github. Check it out - https://github.com/codefuze/js-mobile-tablet-redirect. Feel free to submit issues if there is anything wrong!

Interlanguage answered 31/12, 2011 at 4:34 Comment(2)
This won't work for Android versions > 3.0, such as ICS (4.0). I think the accepted answer is the right one - check whether "Mobile Safari" is in the useragent.Spruce
Not sure about other Kindles, but the Kindle Fire 1 does NOT have 'kindle' in its user-agent string - nor does it contain 'android'; the only clues are the words 'Silk' and 'Silk-Accelerated'.Milt
S
6

Once I have detected Android in the user agent, this is how I differentiate between tablet and smartphone browsers (this is using Python, but is similarly simple for other programming languages):

if ("Android" in agent):
  if ("Mobile" in agent):
    deviceType = "Phone"
  else:
    deviceType = "Tablet"

UPDATED: to reflect use of Chrome on Android, as per comments below.

Spruce answered 29/3, 2012 at 15:51 Comment(3)
Why not just look for "Mobile" in agent?Oniskey
I think that might work as well, but I think it may be dangerous to search for just "Mobile" - there could be devices with names, say "HTC Mobile", etc. I think this is the recommended approach.Spruce
Now that Google released Chrome for Android, your approach might get a little problematic. Because, Chrome on Android will report itself as "Chrome Mobile" instead of the usual "Mobile Safari" as the stock Android browser is doing. For some official recommendations: developers.google.com/chrome/mobile/docs/user-agentOniskey
H
6

Based on Agents strings on this site:

http://www.webapps-online.com/online-tools/user-agent-strings

This results came up:
First:

All Tablet Devices have:
1. Tablet
2. iPad

Second:

All Phone Devices have:
1. Mobile
2. Phone

Third:

Tablet and Phone Devices have:
1. Android

If you can detect level by level, I thing the result is 90 percent true. Like SharePoint Device Channels.

Harvard answered 23/9, 2015 at 9:18 Comment(0)
H
2

Here is what I use:

public static boolean onTablet()
    {
    int intScreenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

    return (intScreenSize == Configuration.SCREENLAYOUT_SIZE_LARGE) // LARGE
    || (intScreenSize == Configuration.SCREENLAYOUT_SIZE_LARGE + 1); // Configuration.SCREENLAYOUT_SIZE_XLARGE
    }
Hilbert answered 11/4, 2012 at 14:54 Comment(0)
O
2

While we can't say if some tablets omit "mobile", many including the Samsung Galaxy Tab do have mobile in their user-agent, making it impossible to detect between an android tablet and android phone without resorting to checking model specifics. This IMHO is a waste of time unless you plan on updating and expanding your device list on a monthly basis.

Unfortunately the best solution here is to complain to Google about this and get them to fix Chrome for Android so it adds some text to identify between a mobile device and a tablet. Hell even a single letter M OR T in a specific place in the string would be enough, but I guess that makes too much sense.

Olvan answered 27/7, 2012 at 15:51 Comment(0)
E
1

If you use the absence of "Mobile" then its almost correct. But there are HTC Sensation 4G (4.3 inch with android 2.X) which does not send Mobile keyword.

The reason why you may want to treat it separately is due to iframes etc.

Elora answered 11/7, 2011 at 18:40 Comment(0)
J
1

Xoom has the word Xoom in the user-agent: Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; Xoom Build/HRI66) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

Galaxy Tab has "Mobile" in the user-agent: Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

So, it's easy to detect the Xoom, hard to detect if a specific Android version is mobile or not.

Jeffry answered 26/7, 2011 at 0:3 Comment(0)
P
1

Most modern tablets run honeycomb aka 3.x No phones run 3.x by default. Most tablets that currently run 2.x have less capacity and might be better of when presented with a mobile site anyway. I know it 's not flawless.. but I guess it 's a lot more accurate than the absence of mobile..

Pleuropneumonia answered 25/9, 2011 at 19:39 Comment(1)
Actually phones cannot even run 3.x because it was a tablet-only release of Android and was not open sourced.Oniskey
U
1

While Mobile Android may have "mobile" in it's user-agent string, what if it's using Opera Mobile for Android on a Tablet? It'll still have "mobile" in it's user-agent string, but should be displaying Tablet-sized sites. You'll need to test for "mobile" that is not preceded by "opera" rather than just "mobile"

or you could just forget about Opera Mobile.

Unforgettable answered 15/12, 2011 at 12:30 Comment(2)
Opera mobile does not use 'mobile' in the user string, it uses 'mobi'. See hereWhity
Opera Mobile for mobiles has "Opera Mobi" in the user agent string, but Opera Mobile for tablets has "Opera Tablet" in the user agent string.Palaeontology
A
1

I would recommend using Categorizr to detect if the user is on a tablet. You can view categorizr test results here.

Awhirl answered 1/3, 2012 at 0:43 Comment(0)
A
0

The 51Degrees beta, 1.0.1.6 and the latest stable release 1.0.2.2 (4/28/2011) now have the ability to sniff for tablet. Basically along the lines of:

string capability = Request.Browser["is_tablet"];

Hope this helps you.

Auroora answered 29/4, 2011 at 3:33 Comment(0)
B
0

Try OpenDDR, it is free unlike most other solutions mentioned.

Babbitt answered 19/2, 2017 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.