How to detect a tablet device in Android?

前端 未结 4 891
无人共我
无人共我 2021-01-03 07:38

I am trying to port my application developed for smartphones to the tablets with minor modifications. Is there an API in Android to detect if the device is tablet?

4条回答
  •  天涯浪人
    2021-01-03 08:22

    place this method in onResume() and can check.

    public double tabletSize() {
    
         double size = 0;
            try {
    
                // Compute screen size
    
                DisplayMetrics dm = context.getResources().getDisplayMetrics();
    
                float screenWidth  = dm.widthPixels / dm.xdpi;
    
                float screenHeight = dm.heightPixels / dm.ydpi;
    
                size = Math.sqrt(Math.pow(screenWidth, 2) +
    
                                     Math.pow(screenHeight, 2));
    
            } catch(Throwable t) {
    
            }
    
            return size;
    
        }
    

    generally tablets starts after 6 inch size.

提交回复
热议问题