How to detect device is Android phone or Android tablet?
Asked Answered
B

8

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.

Bedfordshire answered 4/7, 2012 at 13:46 Comment(6)
Check this answer: https://mcmap.net/q/57347/-tablet-or-phone-androidErotica
possible duplicate of Tablet or Phone - AndroidEskilstuna
possible duplicate of Determine if the device is a smartphone or tablet?Muscarine
Just provide different resource files for different screen sizes and let the system determine it for you. See this answer for more.Murial
Possible duplicate of Tablet or Phone - AndroidHaphazard
Possible duplicate of Navigation Drawer: set as always opened on tabletsYellowbird
F
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");
}
Fanatic answered 14/12, 2015 at 7:27 Comment(3)
This should be the accepted answer, see: #9279611Affranchise
nexus 7 is not picking up from sw600 while I was trying this.Harvison
Nexus 7 picks up w600dp. I also add a w820dp for landscape mode.Kandicekandinsky
C
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.

Chicle answered 4/7, 2012 at 14:22 Comment(7)
One doubt.. galaxy note's screen is considered large..will it be considered as tablet using this code?Joiner
@Raghav If you have any doubts, I'd read this article: developer.android.com/guide/practices/screens_support.html (it explains the difference between the small/medium/large/xlarge buckets, etc.)Chicle
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 nowSimonnesimonpure
Using about code Nexus 7 is NOT considered as a TABLET..!Discontented
@Joiner with the same code Galaxy Note III is a deviceMoorings
Do NOT use this method as it fails to detect Samsung Galaxy Mega device as a phone.Nora
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: developer.android.com/training/multiscreen/…Affranchise
F
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");
}
Fanatic answered 14/12, 2015 at 7:27 Comment(3)
This should be the accepted answer, see: #9279611Affranchise
nexus 7 is not picking up from sw600 while I was trying this.Harvison
Nexus 7 picks up w600dp. I also add a w820dp for landscape mode.Kandicekandinsky
E
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;
    }
Entozoon answered 11/9, 2013 at 12:16 Comment(2)
Your code will not detect devices that have 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.Gavrilla
And then the Nexus 7 will not be recognized as a tablet :(Parrett
S
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;
}
Slot answered 16/7, 2019 at 6:2 Comment(0)
P
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;
}
Photoactive answered 14/9, 2016 at 15:5 Comment(7)
Would this not return true if the tablet had GSM capabilities?Handedness
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.Photoactive
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.Handedness
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.Photoactive
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.Handedness
For layout purposes, anything 7" or larger gets a tablet layout, else a phone layout.Handedness
@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 ?Photoactive
L
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.

Lattie answered 4/7, 2012 at 14:22 Comment(1)
And then if restrict your app to only a list of devices, you eventually keep something out that could run it. Restriction lists are bad.Acherman
D
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            
}
Desmund answered 3/6, 2014 at 13:30 Comment(2)
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 code.google.com/p/chromium/codesearch#chromium/src/ui/android/…Pannonia
Some tablet UA's now contain "Mobile", so you cannot rely on this method - see discussion in comments hereGavrilla
W
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;
}
Wellknown answered 19/8, 2014 at 4:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.