All Things Techie With Huge, Unstructured, Intuitive Leaps

Android - Programmatically Determine if Device is a Tablet

Here is some source code to determine if the Android device is a tablet or a phone:

public boolean isTablet() { 
    try { 
        // Compute screen size 
    Context context = (put the class name of your Activity here eg MyActivity).this;
        DisplayMetrics dm = context.getResources().getDisplayMetrics()
        float screenWidth  = dm.widthPixels / dm.xdpi
        float screenHeight = dm.heightPixels / dm.ydpi
        double size = Math.sqrt(Math.pow(screenWidth, 2) + 
                                Math.pow(screenHeight, 2)); 
        // Tablet devices have a screen size greater than 6 inches 
        return size >= 6; 
    } catch(Throwable t) { 
        Log.e("Failed to compute screen size", t.toString()); 
        return false; 

No comments:

Post a Comment