Created
April 12, 2012 21:49
-
-
Save Richie97/2371251 to your computer and use it in GitHub Desktop.
Tablet Checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if the device is a tablet or a phone | |
* | |
* @param activityContext | |
* The Activity Context. | |
* @return Returns true if the device is a Tablet | |
*/ | |
public static boolean isTabletDevice(Context activityContext) { | |
// Verifies if the Generalized Size of the device is XLARGE to be | |
// considered a Tablet | |
boolean xlarge = ((activityContext.getResources().getConfiguration().screenLayout & | |
Configuration.SCREENLAYOUT_SIZE_MASK) == | |
Configuration.SCREENLAYOUT_SIZE_XLARGE); | |
// If XLarge, checks if the Generalized Density is at least MDPI | |
// (160dpi) | |
if (xlarge) { | |
DisplayMetrics metrics = new DisplayMetrics(); | |
Activity activity = (Activity) activityContext; | |
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); | |
// MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160, | |
// DENSITY_TV=213, DENSITY_XHIGH=320 | |
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) { | |
// Yes, this is a tablet! | |
return true; | |
} | |
} | |
// No, this is not a tablet! | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment