6

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Bostone
  • 36,858
  • 39
  • 167
  • 227
  • There is a Configuration.screenLayout bitmask - see here http://stackoverflow.com/questions/5015094/determine-device-screen-category-small-normal-large-xlarge-using-code – dag Apr 09 '12 at 22:08

2 Answers2

6

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 );
    }
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • What?? I put 7.0 as the input param, and it returns false for my Kindle Fire. – IgorGanapolsky May 23 '12 at 16:23
  • strange, because i tested it on my device and its working perfectly fine. try logging `screenDiagonal` before *return* and see what it actually calculates for your device – waqaslam May 24 '12 at 07:33
  • It returns 6.708203932499369. So you have to take that into account. – IgorGanapolsky May 24 '12 at 23:59
  • @Waqas FYI: your method on Asus Transformer Pad TF300 returns diagonal 8.94(in landscape) & 8.60(in portrait). There is something off. – IgorGanapolsky May 25 '12 at 00:16
  • densityDPI won't be an accurate value so you can't use it this way. For instance, a medium density phone will most often times report a dpi of 160, even though it's physical dpi might be some other value. You can try using xdpi and ydpi but I've found that's not always accurate either, such as on some Samsung devices. It seems to me that there's no great way to figure out the physical size of a screen, which is absurd. – SkolVikingsGuy Jul 23 '12 at 20:56
1

Check out the documentation for Supporting Multiple Screens. You can follow a predetermined folder naming structure so that Android will load different layouts and drawables for different screen sizes/densities.

For example:

res/layout/my_layout.xml             // layout for normal screen size ("default")
res/layout-small/my_layout.xml       // layout for small screen size
res/layout-large/my_layout.xml       // layout for large screen size
res/layout-xlarge/my_layout.xml      // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation

res/drawable-mdpi/my_icon.png        // bitmap for medium density
res/drawable-hdpi/my_icon.png        // bitmap for high density
res/drawable-xhdpi/my_icon.png       // bitmap for extra high density
wsanville
  • 37,158
  • 8
  • 76
  • 101