Getting the Inch Size of the Android Device

Here’s a simple code snippet to calculate approximate physical size of the screen of any Android device. If you find that useful or have suggestions to improve, your thoughts and feedback are welcome in the comments.

/**
 * Calculates an approximation to the diagonal size of the screen. Based on the width and height pixels, the # of pixels in diagonal are calculated and then using the density DPI
 * the pixels are translated into physical dimension in pixels
 *
 * @param activity
 * @return
 */
public static double getDiagonalSize(Activity activity) {
	DisplayMetrics metrics = new DisplayMetrics();

	activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

	return Math.sqrt((metrics.widthPixels * metrics.widthPixels)
			+ (metrics.heightPixels * metrics.heightPixels))
			/ metrics.densityDpi;

}