¿Cómo determinar la categoría de tamaño de pantalla del dispositivo (pequeño, normal, grande, xlarge) usando código?

¿Hay alguna manera de determinar la categoría de tamaño de pantalla del dispositivo actual, como pequeño, normal, grande, xlarge?

No la densidad, sino el tamaño de la pantalla.

10 Solutions collect form web for “¿Cómo determinar la categoría de tamaño de pantalla del dispositivo (pequeño, normal, grande, xlarge) usando código?”

Puede utilizar la máscara de bits Configuration.screenLayout .

Ejemplo:

 if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) { // on a large screen device ... } 

El código abajo ilustra la respuesta anterior, mostrando el tamaño de la pantalla como un Toast.

 //Determine screen size 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(); } 

Este código muestra la densidad de la pantalla como Toast.

 //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(); } 

La respuesta de Jeff Gilfelt como un método auxiliar estático:

 private static String getSizeName(Context context) { int screenLayout = context.getResources().getConfiguration().screenLayout; screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK; switch (screenLayout) { case Configuration.SCREENLAYOUT_SIZE_SMALL: return "small"; case Configuration.SCREENLAYOUT_SIZE_NORMAL: return "normal"; case Configuration.SCREENLAYOUT_SIZE_LARGE: return "large"; case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9 return "xlarge"; default: return "undefined"; } } 

Gracias por las respuestas anteriores, que me ayudó mucho 🙂 Pero para aquellos (como yo) forzado a seguir soportando Android 1.5 podemos utilizar la reflexión de java para compatibilidad con versiones anteriores:

 Configuration conf = getResources().getConfiguration(); int screenLayout = 1; // application default behavior try { Field field = conf.getClass().getDeclaredField("screenLayout"); screenLayout = field.getInt(conf); } catch (Exception e) { // NoSuchFieldException or related stuff } // Configuration.SCREENLAYOUT_SIZE_MASK == 15 int screenType = screenLayout & 15; // Configuration.SCREENLAYOUT_SIZE_SMALL == 1 // Configuration.SCREENLAYOUT_SIZE_NORMAL == 2 // Configuration.SCREENLAYOUT_SIZE_LARGE == 3 // Configuration.SCREENLAYOUT_SIZE_XLARGE == 4 if (screenType == 1) { ... } else if (screenType == 2) { ... } else if (screenType == 3) { ... } else if (screenType == 4) { ... } else { // undefined ... } 

Si desea conocer fácilmente la densidad de pantalla y el tamaño de un dispositivo Android, puede utilizar esta aplicación gratuita (no se requiere permiso): https://market.android.com/details?id=com.jotabout.screeninfo

 private String getDeviceResolution() { int density = mContext.getResources().getDisplayMetrics().densityDpi; switch (density) { case DisplayMetrics.DENSITY_MEDIUM: return "MDPI"; case DisplayMetrics.DENSITY_HIGH: return "HDPI"; case DisplayMetrics.DENSITY_LOW: return "LDPI"; case DisplayMetrics.DENSITY_XHIGH: return "XHDPI"; case DisplayMetrics.DENSITY_TV: return "TV"; case DisplayMetrics.DENSITY_XXHIGH: return "XXHDPI"; case DisplayMetrics.DENSITY_XXXHIGH: return "XXXHDPI"; default: return "Unknown"; } } 

¿Necesita chequear xlarge pantallas y x … altas densidades? Este es el código alterado de la respuesta elegida.

 //Determine screen size 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 if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) { Toast.makeText(this, "XLarge 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 if (density==DisplayMetrics.DENSITY_XHIGH) { Toast.makeText(this, "DENSITY_XHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density==DisplayMetrics.DENSITY_XXHIGH) { Toast.makeText(this, "DENSITY_XXHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density==DisplayMetrics.DENSITY_XXXHIGH) { Toast.makeText(this, "DENSITY_XXXHIGH... 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(); } 

Aquí está una versión de Xamarin.Android de la respuesta de Tom McFarlin

  //Determine screen size if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeLarge) { Toast.MakeText (this, "Large screen", ToastLength.Short).Show (); } else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeNormal) { Toast.MakeText (this, "Normal screen", ToastLength.Short).Show (); } else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeSmall) { Toast.MakeText (this, "Small screen", ToastLength.Short).Show (); } else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeXlarge) { Toast.MakeText (this, "XLarge screen", ToastLength.Short).Show (); } else { Toast.MakeText (this, "Screen size is neither large, normal or small", ToastLength.Short).Show (); } //Determine density DisplayMetrics metrics = new DisplayMetrics(); WindowManager.DefaultDisplay.GetMetrics (metrics); var density = metrics.DensityDpi; if (density == DisplayMetricsDensity.High) { Toast.MakeText (this, "DENSITY_HIGH... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMetricsDensity.Medium) { Toast.MakeText (this, "DENSITY_MEDIUM... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMetricsDensity.Low) { Toast.MakeText (this, "DENSITY_LOW... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMetricsDensity.Xhigh) { Toast.MakeText (this, "DENSITY_XHIGH... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMetricsDensity.Xxhigh) { Toast.MakeText (this, "DENSITY_XXHIGH... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMetricsDensity.Xxxhigh) { Toast.MakeText (this, "DENSITY_XXXHIGH... Density is " + density, ToastLength.Long).Show (); } else { Toast.MakeText (this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + density, ToastLength.Long).Show (); } 

Copie y pegue este código en su Activity y cuando se ejecute, se brindará la categoría de tamaño de pantalla del dispositivo.

 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(); 

¿No podría hacer esto usando un recurso de cadena y enums? Puede definir un recurso de cadena que tenga el nombre del tamaño de pantalla, como SMALL, MEDIUM o LARGE. A continuación, podría utilizar el valor del recurso de cadena para crear una instancia del enum.

  1. Defina un Enum en su código para los diferentes tamaños de pantalla que le interesan.

     public Enum ScreenSize { SMALL, MEDIUM, LARGE,; } 
  2. Defina un recurso de cadena, digamos screensize, cuyo valor será SMALL, MEDIUM o LARGE.

     <string name="screensize">MEDIUM</string> 
  3. Pon una copia de screensize en un recurso de cadena en cada dimensión que te interesa.
    Por ejemplo, <string name="screensize">MEDIUM</string> iría en valores-sw600dp / strings.xml y values-medium / strings.xml y <string name="screensize">LARGE</string> En sw720dp / strings.xml y values-large / strings.xml.
  4. En código, escriba
    ScreenSize size = ScreenSize.valueOf(getReources().getString(R.string.screensize);
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.