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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
ClosDesign
  • 3,894
  • 9
  • 39
  • 62
  • 2
    I'm just guessing, but perhaps there's a way to see how high the viewing resolution is as well? – Phonon Mar 17 '11 at 16:15
  • @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. – ClosDesign Mar 17 '11 at 17:47
  • Not sure if I can help you here. – Phonon Mar 17 '11 at 18:38

15 Answers15

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.

Ouadie
  • 13,005
  • 4
  • 52
  • 62
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 3
    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. – ClosDesign Mar 21 '11 at 15:00
  • Wurfl still has 'mobile' as part of Xoom's user agent: http://www.tera-wurfl.com/explore/?action=wurfl_id&id=motorola_xoom_ver1 – yoavf Jun 16 '11 at 05:06
  • 6
    @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". – CommonsWare Jun 16 '11 at 11:18
  • 29
    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. – vernonk Dec 23 '11 at 03:16
  • Also this is worth looking at: https://developers.google.com/chrome/mobile/docs/user-agent – Eren Tantekin Apr 29 '12 at 22:08
  • 24
    false! samsung galaxy tab is obvious tablet but it has "mobile" in its user agent – puchu Apr 30 '12 at 14:57
  • 1
    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... – Tracker1 Mar 01 '13 at 20:19
  • 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. – mask Jul 29 '13 at 22:39
  • @CommonsWare see in galaxy tab 2 , i have tried use the useragent but the user-Agent header has a Mobile string in the tablet itself – Balaji Sabdhar M Mar 07 '14 at 10:53
  • thank you. Without you I wouldn't have notice this. – Esteban Morales May 31 '19 at 14:29
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.

Bitwalker
  • 211
  • 1
  • 3
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>
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 3
    This works perfectly. I added onto this for iPad and iPhone/iPod. `else if (userAgent.search("ipad") > -1) document.write(" iPad
    ") else if ((userAgent.search("iphone") > -1)||(userAgent.search("ipod") > -1)) document.write(" iPhone or iPod
    ")`
    – Spencer Cole Jun 18 '12 at 12:39
  • 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? – LTech Aug 15 '13 at 18:55
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!

CodeFuze
  • 371
  • 2
  • 6
  • 1
    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. – Suman Mar 29 '12 at 15:49
  • 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'. – mklement0 Dec 10 '12 at 16:15
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.

Jalali Shakib
  • 616
  • 8
  • 17
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.

Suman
  • 9,221
  • 5
  • 49
  • 62
  • 1
    Why not just look for "Mobile" in agent? – Eren Tantekin Apr 29 '12 at 22:21
  • 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. – Suman Apr 30 '12 at 16:25
  • 1
    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: https://developers.google.com/chrome/mobile/docs/user-agent – Eren Tantekin May 02 '12 at 00:57
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
    }
Regis St-Gelais
  • 3,156
  • 5
  • 27
  • 40
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.

awe
  • 21,938
  • 6
  • 78
  • 91
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.

akjoshi
  • 15,374
  • 13
  • 103
  • 121
zel
  • 11
  • 1
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.

travyo
  • 493
  • 4
  • 5
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..

gjr
  • 11
  • 1
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.

Big The Dave
  • 73
  • 1
  • 1
  • 7
  • 1
    Opera mobile does not use 'mobile' in the user string, it uses 'mobi'. See [here](http://www.useragentstring.com/pages/Mobile%20Browserlist/) – Ian Stanway Feb 03 '12 at 09:59
  • 2
    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. – tagawa Apr 23 '12 at 05:17
1

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

hybrid
  • 3,868
  • 2
  • 26
  • 28
0

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

Werner Keil
  • 592
  • 5
  • 12
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.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
Joey Schluchter
  • 2,522
  • 3
  • 22
  • 26