androidandroid-screen

How do I get the ScreenSize programmatically in android


Android defines screen sizes as Normal Large XLarge etc.

It automatically picks between static resources in appropriate folders. I need this data about the current device in my java code. The DisplayMetrics only gives information about the current device density. Nothing is available regarding screen size.

I did find the ScreenSize enum in grep code here However this does not seem available to me for 4.0 SDK. Is there a way to get this information?


Solution

  • Copy and paste this code into your Activity and when it is executed it will Toast the device's screen size category.

    int screenSize = getResources().getConfiguration().screenLayout &
            Configuration.SCREENLAYOUT_SIZE_MASK;
    
    String toastMsg;
    switch(screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            toastMsg = "Large screen";
            break;
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            toastMsg = "Normal screen";
            break;
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
            toastMsg = "Small screen";
            break;
        default:
            toastMsg = "Screen size is neither large, normal or small";
    }
    Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();