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.