3 Jan 2013

Check whether the device is Tablet or Phone - Android

Solution1: calculate the size of the screen and use that to make the decision whether the device is a phone or tablet?


public boolean isTablet(Context context)
    {
boolean xlarge = ((context.getResources().getConfiguration().screenLayout &       Configuration.SCREENLAYOUT_SIZE_MASK) == 4);
boolean large = ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
        return (xlarge || large);
    }


Solution2: calculate the size of the screen diagonal and use that to make the decision whether the device is a phone or tablet?


public boolean isTablet()
       {
              try
              {
                     // Compute screen size
                     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 should have a screen size greater than 6 inches
                     return size >= 9.0;
              }
              catch(Throwable t)
              {
                     Log.error(TAG_LOG, "Failed to compute screen size", t);
                     return false;
              }
       }


 Solution3: determine the oreintation of the device and use that to make the decision whether the device is a phone or tablet?


  
private static boolean isTablet(Display display)
    {
           Log.d(TAG, "isTablet()");
           final int width = display.getWidth();
           final int height = display.getHeight();

           switch (display.getOrientation())
           {
           case 0: case 2:
                  if(width > height) return true;
                  break;
           case 1: case 3:
                  if(width < height) return true;
                  break;
           }
           return false;
    }

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)

2 comments:

  1. Great Example Sami...Helped me a lot..
    Thanks, darshan barot (hope You remember me)

    ReplyDelete
    Replies
    1. yes of course.

      Thank you very much for your feedback

      Delete