Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if the device is a smartphone or tablet? [duplicate]

I would like to get info about a device to see if it's a smartphone or tablet. How can I do it?

I would like to show different web pages from resources based on the type of device:

String s="Debug-infos:"; s += "\n OS Version: " + System.getProperty("os.version") + "(" +    android.os.Build.VERSION.INCREMENTAL + ")"; s += "\n OS API Level: " + android.os.Build.VERSION.SDK; s += "\n Device: " + android.os.Build.DEVICE; s += "\n Model (and Product): " + android.os.Build.MODEL + " ("+ android.os.Build.PRODUCT + ")"; 

However, it seems useless for my case.


This solution works for me now:

DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int width = metrics.widthPixels; int height = metrics.heightPixels;  if (SharedCode.width > 1023 || SharedCode.height > 1023){    //code for big screen (like tablet) }else{    //code for small screen (like smartphone) } 
like image 246
user1001635 Avatar asked Feb 14 '12 14:02

user1001635


People also ask

Is a tablet an Android?

It boils down to this: An Android tablet is a touch-screen, mobile device that runs some version of the Android operating system on it. And it's not a smartphone, though with the right software and hardware you might be able to make calls over Wi-Fi networks using one.

How do I know what Android tablet I have?

Open 'Settings' and then 'About tablet' on the bottom left side. Then you will find 'Device name' (e.g. “Galaxy Tab A (2016)”) and 'Model number' (e.g. “SM-T585”) below. After choosing 'Software information', you'll see the 'Android version' (e.g. “7.0”).


1 Answers

This subject is discussed in the Android Training:

Use the Smallest-width Qualifier

If you read the entire topic, they explain how to set a boolean value in a specific value file (as res/values-sw600dp/attrs.xml):

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

Because the sw600dp qualifier is only valid for platforms above android 3.2. If you want to make sure this technique works on all platforms (before 3.2), create the same file in res/values-xlarge folder:

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

Then, in the "standard" value file (as res/values/attrs.xml), you set the boolean to false:

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

Then in you activity, you can get this value and check if you are running in a tablet size device:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet); if (tabletSize) {     // do something } else {     // do something else } 
like image 123
ol_v_er Avatar answered Sep 21 '22 17:09

ol_v_er