android development tutorial on the way to determine whether a phone or a tablet

  • 2020-05-30 21:03:45
  • OfStack

Method 1


public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

Method 2

Determine whether it is a phone or a tablet by calculating the size of the device:


/**
 *  Determine whether it is a flat plate 
 * 
 * @return
 */
private boolean isPad() {
 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
 Display display = wm.getDefaultDisplay();
 //  The width of the screen 
 float screenWidth = display.getWidth();
 //  The screen height 
 float screenHeight = display.getHeight();
 DisplayMetrics dm = new DisplayMetrics();
 display.getMetrics(dm);
 double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
 double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
 //  The screen size 
 double screenInches = Math.sqrt(x + y);
 //  Is greater than 6 Size is Pad
 if (screenInches >= 6.0) {
  return true;
 }
 return false;
}


Related articles: