android – Get screen size bucket programmatically?-ThrowExceptions

Exception or error:

I want to determine whether the current device has a small, medium, large or xlarge screen in code. I can’t find anything in the SDK docs that would help me get to that information. All the methods / classes I have looked at provide only absolute values (i.e. screen size in pixels, screen density, etc.).

Is there a way to tell what kind of screen I’m running on in code?

How to solve:

I ended up using bool resources placed in the different bucket folders. I only needed to differentiate between normal (small / medium) and large (large / xlarge) screens, so I did this:


<?xml version="1.0" encoding="utf-8"?>
    <bool name="screen_large">false</bool>


<?xml version="1.0" encoding="utf-8"?>
    <bool name="screen_large">true</bool>

Then, I just call getBoolean(R.bool.screen_large) to tell whether the screen is large or not. This way it’s 100% up to the platform decide what screen the device has.


There is a difference between Density and screen type.
enter image description here

Since you can get the pixels and the density you can always have a static Helper class that determines that.

You can transofm pixels to dp with that

public static float dpFromPixels(int pixels) {
        float dp = (float) ((pixels) / Density.scale);
        return dp;

I think that you might want to add or subtract .5f from the pixels since getting pixels from dp comes from that code.

public static int pixelsFromDp(float dps) {
        int pixels = (int) (dps * Density.scale + 0.5f);
        return pixels;

From the documentation

xlarge screens are at least 960dp x 720dp

large screens are at least 640dp x 480dp

normal screens are at least 470dp x 320dp

small screens are at least 426dp x 320dp


public static boolean isLargeScreen(Context context) {
  int screenSize = context.getResources().getConfiguration().screenLayout
    & Configuration.SCREENLAYOUT_SIZE_MASK;
  return screenSize >= Configuration.SCREENLAYOUT_SIZE_LARGE;


I agree that Felix answer is pretty elegant. But I think the way I found in this post might be even easier How to determine device screen size category (small, normal, large, xlarge) using code?


Use the DisplayMetrics class…

DisplayMetrics dm = new DisplayMetrics();

int density = dm.densityDpi;

Small = DisplayMetrics.DENSITY_LOW

Medium = DisplayMetrics.DENSITY_MEDIUM

Large = DisplayMetrics.DENSITY_HIGH

XLarge = DisplayMetrics.DENSITY_XHIGH


I use this method in my code to differeniate between “Large Screens” (What I consider tablets) and “Small Screens” (What I consider phones).

public static boolean isLargeScreen(Configuration toCheckConfig) {  
    int screenSizeBucket = toCheckConfig.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
    if (screenSizeBucket >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
        return true;
    else return false;

The Configuration object also contains SCREENLAYOUT_SIZE_SMALL, SCREENLAYOUT_SIZE_XLARGE etc. (if you specifically need to test for size buckets).

This also works well with these two util functions:

public static int getPixelsFromDp(Context context, int dpValue) {
    return (int) (context.getResources().getDisplayMetrics().density * dpValue);

public static int getDpfromPixels(Context context, int pixels) {
    return (int) (pixels / context.getResources().getDisplayMetrics().density);

Where context.getResources().getDisplayMetrics().density is equal to 1.0, 1.5, 2.0 (mdpi, hdpi, xhdpi respectively).


Just as a complement to the Felix’s answer, you can use the following code to get the ScreenSize without creating any folder for values-XXX :

int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
        Log.d(TAG, "screenSize = "+screenSize);

        switch(screenSize) {
            case Configuration.SCREENLAYOUT_SIZE_LARGE:
                Log.d(TAG, "Large Screen");
            case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                Log.d(TAG, "Normal Screen");
            case Configuration.SCREENLAYOUT_SIZE_SMALL:
                Log.d(TAG, "Small Screen");
            case Configuration.SCREENLAYOUT_SIZE_XLARGE : 
                Log.d(TAG, "XLarge Screen");
                Log.d(TAG,  "Screen size is neither large, normal or small or xlarge");


The DisplayMetrics has got all that you need. To get it

 DisplayMetrics metrics = new DisplayMetrics();

Leave a Reply

Your email address will not be published. Required fields are marked *