60

I have two apps for Android tablets and Android phones. For tablet app I set android:minSdkVersion="11". But nowadays Android phones like Galaxy S3 has Android version 4.0.4 so S3 users can download my tablet app from Google Play Store. I want to warn phone users to download phone app when they install tablet app. Vice versa for tablet users download tablet app when they run phone app.

Is there any easy way to detect the device type?

Edit:

I found solution on this link.

In your manifest file you can declare screen feature for handset and tablets then Google Play decides download permissions for both phone and tablet.

JJD
  • 50,076
  • 60
  • 203
  • 339
KAPLANDROID
  • 1,099
  • 1
  • 12
  • 23
  • Check this answer: http://stackoverflow.com/a/9590506/580131 – Stefano Ortisi Jul 04 '12 at 13:49
  • 1
    possible duplicate of [Tablet or Phone - Android](http://stackoverflow.com/questions/5832368/tablet-or-phone-android) – Moog Jul 04 '12 at 13:50
  • possible duplicate of [Determine if the device is a smartphone or tablet?](http://stackoverflow.com/questions/9279111/determine-if-the-device-is-a-smartphone-or-tablet) – bummi May 06 '14 at 08:40
  • Just provide different resource files for different screen sizes and let the system determine it for you. See [this answer](http://stackoverflow.com/a/41646301/3681880) for more. – Suragch Jan 14 '17 at 02:50
  • Possible duplicate of [Tablet or Phone - Android](https://stackoverflow.com/questions/5832368/tablet-or-phone-android) – Gabor Jan 09 '18 at 21:16
  • Possible duplicate of [Navigation Drawer: set as always opened on tablets](https://stackoverflow.com/a/50646711/7594961) – Bertram Gilfoyle Jun 25 '19 at 17:51

8 Answers8

132

Use this:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

which will return true if the device is operating on a large screen.

Some other helpful methods can be found here.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • 1
    One doubt.. galaxy note's screen is considered large..will it be considered as tablet using this code? – Rookie Feb 24 '13 at 17:33
  • @Raghav If you have any doubts, I'd read this article: http://developer.android.com/guide/practices/screens_support.html (it explains the difference between the small/medium/large/xlarge buckets, etc.) – Alex Lockwood Feb 24 '13 at 18:14
  • 8
    Still, I think that the code provided may wrongly classify large handhelds as tablets... Obviously, the differences between phones and tablets are becoming very blurry so this should be less relevant now – miguelSantirso May 22 '13 at 09:38
  • @AlexLockwood what if device is an OTT device like :- http://www.ebay.in/itm/Android-Cozyswan-S400-Mini-PC-RK3188-Quad-Core-2GB-8GB-Bluetooth-Smart-TV-Box-/171095681607 then how to identify it ?? – r4jiv007 Feb 10 '14 at 11:55
  • 4
    Using about code Nexus 7 is NOT considered as a TABLET..! – Nirav Dangi Feb 18 '14 at 09:30
  • @Rookie with the same code Galaxy Note III is a device – TooCool Aug 08 '14 at 16:04
  • 6
    Do NOT use this method as it fails to detect Samsung Galaxy Mega device as a phone. – Youngjae May 19 '15 at 12:12
  • 1
    This used to work, but I have found it is no longer the correct method. – Clive Jefferies Jan 04 '16 at 14:05
  • @CliveJefferies care to elaborate *why* it's not working and what's the correct method? – TWiStErRob Jul 22 '16 at 12:14
  • I have no idea why it stopped working as we had been using it for a while, but I found that it was no longer correctly classifying tablets and used the method that Bhavana suggested. See: https://developer.android.com/training/multiscreen/screensizes.html#TaskUseSWQuali – Clive Jefferies Jul 22 '16 at 13:25
  • i believe this code will also regard a tv as a tablet or else please correct? – Muhammad Babar Aug 18 '17 at 05:26
  • can i check device is tablet without context? Thanks – famfamfam Mar 09 '21 at 11:00
28

You can also try this
Add boolean parameter in resource file.
in res/values/dimen.xml file add this line

<bool name="isTab">false</bool>

while in res/values-sw600dp/dimen.xml file add this line:

<bool name="isTab">true</bool>

then in your java file get this value:

if(getResources().getBoolean(R.bool.isTab)) {
    System.out.println("tablet");
} else {
    System.out.println("mobile");
}
Bhavana Vadodariya
  • 2,287
  • 1
  • 19
  • 26
7

This code snippet will tell whether device type is 7" Inch or more and Mdpi or higher resolution. You can change the implementation as per your need.

 private static boolean isTabletDevice(Context activityContext) {
        boolean device_large = ((activityContext.getResources().getConfiguration().screenLayout &
                Configuration.SCREENLAYOUT_SIZE_MASK) ==
                Configuration.SCREENLAYOUT_SIZE_LARGE);

        if (device_large) {
            DisplayMetrics metrics = new DisplayMetrics();
            Activity activity = (Activity) activityContext;
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

            if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
                    || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
                    || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
                    || metrics.densityDpi == DisplayMetrics.DENSITY_TV
                    || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {
                AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-True");
                return true;
            }
        }
        AppInstance.getLogger().logD("DeviceHelper","IsTabletDevice-False");
        return false;
    }
Pawan Maheshwari
  • 15,088
  • 1
  • 48
  • 50
  • 2
    Your code will not detect devices that have [SCREENLAYOUT_SIZE_XLARGE](http://developer.android.com/reference/android/content/res/Configuration.html#SCREENLAYOUT_SIZE_XLARGE) as tablets (e.g. Nexus 10). You should change the comparison to `Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE)` to fix this. – DaveAlden Apr 30 '15 at 08:50
  • 1
    And then the Nexus 7 will not be recognized as a tablet :( – shkschneider Jul 13 '15 at 09:20
2

If you want to decide device is tablet or phone based on screen inch, you can use following

device 6.5 inches or higher consider as tablet, but some recent handheld phone has higher diagonal value. good thing with following solution you can set the margin.

public boolean isDeviceTablet(){
    DisplayMetrics metrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float yInches= metrics.heightPixels/metrics.ydpi;
    float xInches= metrics.widthPixels/metrics.xdpi;
    double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);
    if (diagonalInches>=6.5) {
        return true;
    }
    return false;
}
UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40
1

I think this should detect if something is able to make a phonecall, everything else would be a tablet/TV without phone capabilities.

As far as I have seen this is the only thing not relying on screensize.

public static boolean isTelephone(){
    //pseudocode, don't have the copy and paste here right know and can't remember every line
    return new Intent(ACTION_DIAL).resolveActivity() != null;
}
HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37
  • 1
    Would this not return true if the tablet had GSM capabilities? – mwieczorek Sep 20 '16 at 20:22
  • It should only resolve if you have the capacity to call someone. I don't have the capacity to test that thought. IDK what GSM is, but the other solutions can run into problems due to screensize, afaik ? So maybe a combination of both would be the way to go. – HopefullyHelpful Sep 20 '16 at 20:37
  • Sorry, GSM is used more in Asia, where I live. Replace with "Telephony". I have a Samsung Galaxy Tab 8.4 with GSM and mobile data capabilities. On my tablet, this method would return true. – mwieczorek Sep 20 '16 at 20:50
  • Well, I think definitions between phone and tablet is wacky. Both are essentially the same. Just one tends to be bigger and the other one can phone. Depends on what your code is doing. I personally needed the above method to make sure no error is thrown when I try to call someone. In the end if it's just about the layout than checking the size is probably enough. I don't see why you would want to treat a bigger phone different from a tablet. – HopefullyHelpful Sep 20 '16 at 21:07
  • 1
    I'd also like to add - The usage for this would be to find out if a device is a tablet for LAYOUT purposes. Using a telephony-related class to determine something being used for layout is anti-pattern and not reliable - a hack, at best. The DeviceMetrics class should be the catalyst for any code that will eventually influence the view, because that's what it was designed for. This would fail on any tablet with a dialer... which would be half of them in Asia. – mwieczorek Sep 20 '16 at 21:07
  • For layout purposes, anything 7" or larger gets a tablet layout, else a phone layout. – mwieczorek Sep 20 '16 at 21:09
  • @mwieczorek, very well, this makes sense for layout purposes, I stumbled upon this question when trying to figure out a reliable way out to know if something was capable of making a phonecall, so I left the answer here. The question does not include the word layout either. Maybe it should be split in two pieces ? – HopefullyHelpful Jun 20 '17 at 00:06
0

Use Google Play store capabilities and only enable to download your tablet app on tablets and the phone app on phones.

If the user then installs the wrong app then they must have installed using another method.

Marcio Covre
  • 4,556
  • 2
  • 22
  • 24
0

We had the similar issue with our app that should switch based on the device type - Tab/Phone. IOS gave us the device type perfectly but the same idea wasn't working with Android. the resolution/ DPI method failed with small res tabs, high res phones. after a lot of torn hairs and banging our heads over the wall, we tried a weird idea and it worked exceptionally well that won't depend on the resolutions. this should help you too.

in the Main class, write this and you should get your device type as null for TAB and mobile for Phone.

String ua=new WebView(this).getSettings().getUserAgentString();


if(ua.contains("Mobile")){
   //Your code for Mobile
}else{
   //Your code for TAB            
}
Zong
  • 6,160
  • 5
  • 32
  • 46
  • 3
    This is a pretty crazy way to do this, since "new WebView(this)" will actually initialise the entire WebView, which may use tens of megabytes of memory and create a dozen or more threads, and will continue to use all those resources even after the WebView is destroyed. The WebView simply checks if the smallest screen dimension is >=600dp, which you could just do yourself much more efficiently, see https://code.google.com/p/chromium/codesearch#chromium/src/ui/android/java/src/org/chromium/ui/base/DeviceFormFactor.java&q=DeviceFormFactor&sq=package:chromium&type=cs&l=14 – Torne Mar 11 '15 at 14:29
  • 2
    Some tablet UA's now contain "Mobile", so you cannot rely on this method - see discussion in comments [here](http://stackoverflow.com/a/5344382/777265) – DaveAlden Apr 30 '15 at 08:57
0

Use following code to identify device type.

private boolean isTabletDevice() {
     if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb
         // test screen size, use reflection because isLayoutSizeAtLeast is only available since 11
         Configuration con = getResources().getConfiguration();
         try {
              Method mIsLayoutSizeAtLeast = con.getClass().getMethod("isLayoutSizeAtLeast");
              Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE
              return r;
         } catch (Exception x) {
              return false;
         }
      }
    return false;
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
Vaishali Sutariya
  • 5,093
  • 30
  • 32