Is there a way to check if the user is using a tablet or a phone? I\'ve got problems with my tilt function and my new tablet (Transformer)
The other answers list many ways of programmatically determining whether the device is a phone or tablet. However, if you read the documentation, that is not the recommended way to support various screen sizes.
Instead, declare different resources for tablets or phones. You do this my adding additional resource folders for layout
, values
, etc.
For Android 3.2 (API level 13) on, add a sw600dp
folder. This means the smallest width is at least 600dp, which is approximately the phone/tablet divide. However, you can also add other sizes as well. Check out this answer for an example of how to add an additional layout resource file.
If you are also supporting pre Android 3.2 devices, then you will need to add large
or xlarge
folders to support tablets. (Phones are generally small
and normal
.)
Here is an image of what your resources might like after adding an extra xml files for different screen sizes.
When using this method, the system determines everything for you. You don't have to worry about which device is being used at run time. You just provide the appropriate resources and let Android do all the work.
Notes
Use this method which returns true when the device is a tablet
public boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
I needed to detect the smartphone/tablet only in the layout file, because I'm using the navigation code.
What I did first was to create a layout-sw600dp directory but it was not working well because it would activate on my Nokia 8 in landscape mode, but the screen height would be too small.
So, I renamed the directory as layout-sw600dp-h400dp and then I got the desired effect. The h-xxxdp parameter should depend on how much content you want to drop on your layout and as such should be application dependent.
com.sec.feature.multiwindow.tablet in package manager is specific only to tablet and com.sec.feature.multiwindow.phone is specific to phone.
Thinking on the "new" acepted directories (values-sw600dp for example) i created this method based on the screen' width DP:
public static final int TABLET_MIN_DP_WEIGHT = 450;
protected static boolean isSmartphoneOrTablet(Activity act){
DisplayMetrics metrics = new DisplayMetrics();
act.getWindowManager().getDefaultDisplay().getMetrics(metrics);
int dpi = 0;
if (metrics.widthPixels < metrics.heightPixels){
dpi = (int) (metrics.widthPixels / metrics.density);
}
else{
dpi = (int) (metrics.heightPixels / metrics.density);
}
if (dpi < TABLET_MIN_DP_WEIGHT) return true;
else return false;
}
And on this list you can find some of the DP of popular devices and tablet sizes:
Wdp / Hdp
GALAXY Nexus: 360 / 567
XOOM: 1280 / 752
GALAXY NOTE: 400 / 615
NEXUS 7: 961 / 528
GALAXY TAB (>7 && <10): 1280 / 752
GALAXY S3: 360 / 615
Wdp = Width dp
Hdp = Height dp
It is getting increasingly harder to draw the line between phone and tablet. For instance (as of Aug 2015) the Samsung Mega 6.3 device pulls resources from the sw600dp folders -- so as far as Android is concerned it is a tablet.
The answer from @Vyshnavi works in all devices we have tested but not for Mega 6.3.
@Helton Isac answer above returns the Mega 6.3 as a phone -- but since the device still grabs resources from sw600dp it can cause other issues - for instance if you use a viewpager for phones and not for tablets you'll wind up with NPE errors.
In the end it just seems that there are too many conditions to check for and we may just have to accept that some phones are actually tablets :-P