Search code examples
androidandroid-layout

What is the best way to determine if the device is tablet?


I need to write a method to determine if the device is a tablet or a phone. I don't need to display a different user interface depending on this. I only need information about the device, so that in the future I could send it to the metric.

On the Internet, I found many ways to determine if the device is a tablet. I have tested all these methods and they work. Of course, I could not test on all kinds of devices. So I'd like to know which is the best and most accurate way to determine if a device is a tablet.

This is a list of the methods I have been able to find:

1) Use the smallest width qualifier

in res/values-sw600dp/attrs.xml:

<resources>
    <bool name="isTablet">true</bool>
</resources>

in res/values/attrs.xml

<resources>
    <bool name="isTablet">false</bool>
</resources>

And than:

fun isTablet() = context.resources.getBoolean(R.bool.isTablet)

I have concerns about this way. Might be worth adding a resource res/values-sw720dp/attrs.xml with:

<resources>
        <bool name="isTablet">true</bool>
    </resources>

2) Using TelephonyManager

  fun isTablet(context: Context) =
        with(context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager) {
            this.phoneType == TelephonyManager.PHONE_TYPE_NONE
        }

3) Using Configuration:

fun isTablet(context: Context): Boolean {
    return ((context.resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE)
}

4) Using DisplayMetrics:

 fun isTablet(activity: Activity): Boolean {
        val metrics = DisplayMetrics()
        activity.windowManager.defaultDisplay.getMetrics(metrics)
        val yInches = metrics.heightPixels / metrics.ydpi
        val xInches = metrics.widthPixels / metrics.xdpi
        val diagonalInches = sqrt((xInches * xInches + yInches * yInches).toDouble())
        return diagonalInches >= 6.5
    }

Please help me find the best way that will work 100% of the time.


Solution

  • Those are totally different things for the most part.

    Method 1 is checking for the length of the smallest side of the device screen. Basically it's saying a tablet is anything over 4 inches on the smallest size (which is going to be at least inch diagonal on any normal screen resolution).

    Method 2 is saying that a tablet is anything without the ability to make a call.

    Method 3 is checking that its at least 480x640 dp, or at least 3 inches by 4 inches. (Which can be as small as a 5 inch diagonal).

    Method 4 is measuring the diagonal exactly. Which eliminates some of the theoretical problems on method 1, but its does a lot of pointless math (and this implementation isn't optimized).

    All of this depends on what you define as a tablet. There's no actual definition. Method 1,3, and 4 are size based. Method 2 is based on the idea that if it had the ability to call, you'd call it a phone not a tablet.

    Assuming you want a size based solution, I'd go with 1. It's more accurate and more extendable that method 3 (which wouldn't be able to tell a small tablet from a large), and faster than method 4 (and the corner cases where something could pass method 1 but method 4 catches them really don't happen in the real world).