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.

  • Android Libraries in Android Studio
  • 'Uncaught exception thrown by finalizer' when opening MapActivity
  • Problems including Apache HttpComponents in Android Gradle project
  • How to remove specific permission when build Android app with gradle?
  • onNavigationItemSelected in ActionBar is being called at startup how can avoid it?
  • AppBarLayout with FrameLayout container as scrolling content doesn't work
  • 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?

    Related posts:

    How can I save colors in array.xml and get its back to Color array
    How to encode a WAV to a mp3 on a Android device
    Find out why an Android app is being uninstalled
    How can I properly connect an Android application to an RFCOMM socket on a Bluetooth enabled Arduino...
    Need number only soft keyboard?
    Click inside layout redirects to another layout
  • How to put Google Maps V2 on a Fragment Using ViewPager
  • Installing phonegap app to android emulator using windows CLI
  • List supported authorites for selected accounts
  • How to create UUID from string in android
  • Prevent closing by back button in xamarin forms on android
  • What is the difference in contacts versus raw-contacts?
  • 10 Solutions collect form web for “How do I get the ScreenSize programmatically in android”

    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();
    
    private static String getScreenResolution(Context context)
    {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
    
        return "{" + width + "," + height + "}";
    }
    

    Determine Screen Size :

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

    Determine density:

    int density= getResources().getDisplayMetrics().densityDpi;
       switch(density)
      {
      case DisplayMetrics.DENSITY_LOW:
         Toast.makeText(context, "LDPI", Toast.LENGTH_SHORT).show();
          break;
      case DisplayMetrics.DENSITY_MEDIUM:
           Toast.makeText(context, "MDPI", Toast.LENGTH_SHORT).show();
          break;
      case DisplayMetrics.DENSITY_HIGH:
          Toast.makeText(context, "HDPI", Toast.LENGTH_SHORT).show();
          break;
      case DisplayMetrics.DENSITY_XHIGH:
           Toast.makeText(context, "XHDPI", Toast.LENGTH_SHORT).show();
          break;
      }
    

    for Ref:
    http://devl-android.blogspot.in/2013/10/wifi-connectivity-and-hotspot-in-android.html

    You can get display size in pixels using this code.

    Display display = getWindowManager().getDefaultDisplay();
    SizeUtils.SCREEN_WIDTH = display.getWidth();
    SizeUtils.SCREEN_HEIGHT = display.getHeight();
    

    You can try this , It is working Example

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int ht = displaymetrics.heightPixels;
    int wt = displaymetrics.widthPixels;
    
    if ((getResources().getConfiguration().screenLayout &     Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
    Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();}
    
      else if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
    Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
            .show();
    
      } else if ((getResources().getConfiguration().screenLayout &        Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
    Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
            .show();
     } else {
    Toast.makeText(this,
            "Screen size is neither large, normal or small",
            Toast.LENGTH_LONG).show();
     }
    
     // Determine density
     DisplayMetrics metrics = new DisplayMetrics();
     getWindowManager().getDefaultDisplay().getMetrics(metrics);
     int density = metrics.densityDpi;
    
     if (density == DisplayMetrics.DENSITY_HIGH) {
    Toast.makeText(this,
            "DENSITY_HIGH... Density is " + String.valueOf(density),
            Toast.LENGTH_LONG).show();
     } else if (density == DisplayMetrics.DENSITY_MEDIUM) {
    Toast.makeText(this,
            "DENSITY_MEDIUM... Density is " + String.valueOf(density),
            Toast.LENGTH_LONG).show();
     } else if (density == DisplayMetrics.DENSITY_LOW) {
    Toast.makeText(this,
            "DENSITY_LOW... Density is " + String.valueOf(density),
            Toast.LENGTH_LONG).show();
     } else {
    Toast.makeText(
            this,
            "Density is neither HIGH, MEDIUM OR LOW.  Density is "
                    + String.valueOf(density), Toast.LENGTH_LONG)
            .show();
     }
    
     // These are deprecated
     Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
        .getDefaultDisplay();
     int  width = display.getWidth();
     int height = display.getHeight();
    

    simon-

    Different screen sizes have different pixel densities. A 4 inch display on your phone could have more or less pixels then say a 26 inch TV. If Im understanding correctly he wants to detect which of the size groups the current screen is, small, normal, large, and extra large. The only thing I can think of is to detect the pixel density and use that to determine the actual size of the screen.

    I need this for a couple of my apps and the following code was my solution to the problem. Just showing the code inside onCreate. This is a stand alone app to run on any device to return the screen info.

    setContentView(R.layout.activity_main);
        txSize = (TextView) findViewById(R.id.tvSize);
    
        density = (TextView) findViewById(R.id.density);
        densityDpi = (TextView) findViewById(R.id.densityDpi);
        widthPixels = (TextView) findViewById(R.id.widthPixels);
        xdpi = (TextView) findViewById(R.id.xdpi);
        ydpi = (TextView) findViewById(R.id.ydpi);
    
        Configuration config = getResources().getConfiguration();
    
        if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
            Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
            txSize.setText("Large screen");
        } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
            Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
                    .show();
            txSize.setText("Normal sized screen");
        } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
            Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
                    .show();
            txSize.setText("Small sized screen");
        } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
            Toast.makeText(this, "xLarge sized screen", Toast.LENGTH_LONG)
                    .show();
            txSize.setText("Small sized screen");
        } else {
            Toast.makeText(this,
                    "Screen size is neither large, normal or small",
                    Toast.LENGTH_LONG).show();
            txSize.setText("Screen size is neither large, normal or small");
        }
    
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
    
        Log.i(TAG, "density :" + metrics.density);
        density.setText("density :" + metrics.density);
    
        Log.i(TAG, "D density :" + metrics.densityDpi);
        densityDpi.setText("densityDpi :" + metrics.densityDpi);
    
        Log.i(TAG, "width pix :" + metrics.widthPixels);
        widthPixels.setText("widthPixels :" + metrics.widthPixels);
    
        Log.i(TAG, "xdpi :" + metrics.xdpi);
        xdpi.setText("xdpi :" + metrics.xdpi);
    
        Log.i(TAG, "ydpi :" + metrics.ydpi);
        ydpi.setText("ydpi :" + metrics.ydpi);
    

    And a simple XML file

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    
    <TextView
        android:id="@+id/tvSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/density"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/densityDpi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/widthPixels"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/xdpi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/ydpi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    

    I think it is a pretty straight forward simple piece of code!

    public Map<String, Integer> deriveMetrics(Activity activity) {
        try {
            DisplayMetrics metrics = new DisplayMetrics();
    
            if (activity != null) {
                activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            }
    
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("screenWidth", Integer.valueOf(metrics.widthPixels));
            map.put("screenHeight", Integer.valueOf(metrics.heightPixels));
            map.put("screenDensity", Integer.valueOf(metrics.densityDpi));
    
            return map;
        } catch (Exception err) {
            ; // just use zero values
            return null;
        }
    }
    

    This method now can be used anywhere independently. Wherever you want to get information about device screen do it as follows:

            Map<String, Integer> map = deriveMetrics2(this);
            map.get("screenWidth");
            map.get("screenHeight");
            map.get("screenDensity");
    

    Hope this might be helpful to someone out there and may find it easier to use.
    If I need to re-correct or improve please don’t hesitate to let me know! 🙂

    Cheers!!!

    I must be missing something. DisplayMetrics.widthPixels, DisplayMetrics.heightPixels?

    http://developer.android.com/reference/android/util/DisplayMetrics.html#heightPixels

    [EDIT]

    Ah, I am missing something. A brain! +1 for Alex.

    DisplayMetrics displayMetrics = new DisplayMetrics();
    
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    
    int width = displayMetrics.widthPixels;
    
    int height = displayMetrics.heightPixels;
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.