Search code examples
androidtabletscreen-sizeandroid-screen-support

Android - detect small tablet vs big phone?


The app I'm developing contains 2 separate layouts: one is for the regular phones, other for small tablets such as NOOKcolor. The decision which is which is made based on the screen width resolution (currently 600dip). It looks great on Nook but terrible on HTC Rezound, which has a 720 x 1280 display. On the latter, regardless of higher resolution, everything (text, images, etc) look much larger so it gets all bunched up.

What would be a good approach to pick the right device? Perhaps detect physical size (4.3" vs 7") vs resolution?


Solution

  • Use the following method to detect your device's screen size:

        /**
         * Checks if the screen size is equal or above given length
         * @param activity activity screen
         * @param screen_size diagonal size of screen, for example 7.0 inches
         * @return True if its equal or above, else false
         */
        public static boolean checkScreenSize(Activity activity, double screen_size)
        {
            Display display = activity.getWindowManager().getDefaultDisplay();
            DisplayMetrics displayMetrics = new DisplayMetrics();
            display.getMetrics(displayMetrics);
    
            int width = displayMetrics.widthPixels / displayMetrics.densityDpi;
            int height = displayMetrics.heightPixels / displayMetrics.densityDpi;
    
            double screenDiagonal = Math.sqrt( width * width + height * height );
            return (screenDiagonal >= screen_size );
        }