Comment déterminer la catégorie de taille d’écran du périphérique (petite, normale, grande, grande) à l’aide de code?

Existe-t-il un moyen de déterminer la catégorie de taille d’écran du périphérique actuel, telle que small, normal, large, xlarge?

Pas la densité, mais la taille de l’écran.

Vous pouvez utiliser le masque de Configuration.screenLayout .

Exemple:

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

Le code ci-dessous précise la réponse ci-dessus, affichant la taille de l’ écran en tant que 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(); } 

Ce code ci-dessous affiche la densité de l’ écran en tant que Toast.

 //Determine density DisplayMesortingcs mesortingcs = new DisplayMesortingcs(); getWindowManager().getDefaultDisplay().getMesortingcs(mesortingcs); int density = mesortingcs.densityDpi; if (density == DisplayMesortingcs.DENSITY_HIGH) { Toast.makeText(this, "DENSITY_HIGH... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density == DisplayMesortingcs.DENSITY_MEDIUM) { Toast.makeText(this, "DENSITY_MEDIUM... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density == DisplayMesortingcs.DENSITY_LOW) { Toast.makeText(this, "DENSITY_LOW... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } 

La réponse de Jeff Gilfelt en tant que méthode d’assistance statique:

 private static Ssortingng 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"; } } 

Merci pour les réponses ci-dessus, cela m’a beaucoup aidé 🙂 Mais pour ceux (comme moi) obligés de toujours supporter Android 1.5, nous pouvons utiliser la reflection java pour la compatibilité descendante:

 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 ... } 
 private Ssortingng getDeviceResolution() { int density = mContext.getResources().getDisplayMesortingcs().densityDpi; switch (density) { case DisplayMesortingcs.DENSITY_MEDIUM: return "MDPI"; case DisplayMesortingcs.DENSITY_HIGH: return "HDPI"; case DisplayMesortingcs.DENSITY_LOW: return "LDPI"; case DisplayMesortingcs.DENSITY_XHIGH: return "XHDPI"; case DisplayMesortingcs.DENSITY_TV: return "TV"; case DisplayMesortingcs.DENSITY_XXHIGH: return "XXHDPI"; case DisplayMesortingcs.DENSITY_XXXHIGH: return "XXXHDPI"; default: return "Unknown"; } } 

Si vous souhaitez connaître facilement la densité d’écran et la taille d’un appareil Android, vous pouvez utiliser cette application gratuite (sans autorisation): https://market.android.com/details?id=com.jotabout.screeninfo

Besoin de vérifier les écrans xlarge et les densités élevées? Ceci est le code modifié de la réponse choisie.

 //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 DisplayMesortingcs mesortingcs = new DisplayMesortingcs(); getWindowManager().getDefaultDisplay().getMesortingcs(mesortingcs); int density = mesortingcs.densityDpi; if (density==DisplayMesortingcs.DENSITY_HIGH) { Toast.makeText(this, "DENSITY_HIGH... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density==DisplayMesortingcs.DENSITY_MEDIUM) { Toast.makeText(this, "DENSITY_MEDIUM... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density==DisplayMesortingcs.DENSITY_LOW) { Toast.makeText(this, "DENSITY_LOW... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density==DisplayMesortingcs.DENSITY_XHIGH) { Toast.makeText(this, "DENSITY_XHIGH... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density==DisplayMesortingcs.DENSITY_XXHIGH) { Toast.makeText(this, "DENSITY_XXHIGH... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else if (density==DisplayMesortingcs.DENSITY_XXXHIGH) { Toast.makeText(this, "DENSITY_XXXHIGH... Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + Ssortingng.valueOf(density), Toast.LENGTH_LONG).show(); } 

Voici une version de Xamarin.Android de la réponse 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 DisplayMesortingcs mesortingcs = new DisplayMesortingcs(); WindowManager.DefaultDisplay.GetMesortingcs (mesortingcs); var density = mesortingcs.DensityDpi; if (density == DisplayMesortingcsDensity.High) { Toast.MakeText (this, "DENSITY_HIGH... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMesortingcsDensity.Medium) { Toast.MakeText (this, "DENSITY_MEDIUM... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMesortingcsDensity.Low) { Toast.MakeText (this, "DENSITY_LOW... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMesortingcsDensity.Xhigh) { Toast.MakeText (this, "DENSITY_XHIGH... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMesortingcsDensity.Xxhigh) { Toast.MakeText (this, "DENSITY_XXHIGH... Density is " + density, ToastLength.Long).Show (); } else if (density == DisplayMesortingcsDensity.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 (); } 

Ne pourriez-vous pas faire cela en utilisant une ressource de chaîne et des énumérations? Vous pouvez définir une ressource de chaîne portant le nom de la taille de l’écran, telle que SMALL, MEDIUM ou LARGE. Vous pouvez ensuite utiliser la valeur de la ressource de chaîne pour créer une instance de l’énumération.

  1. Définissez un Enum dans votre code pour les différentes tailles d’écran qui vous intéressent.

     public Enum ScreenSize { SMALL, MEDIUM, LARGE,; } 
  2. Définissez une ressource de chaîne, disons screensize, dont la valeur sera SMALL, MEDIUM ou LARGE.

     MEDIUM 
  3. Placez une copie de screensize dans une ressource de chaîne dans chaque dimension qui vous intéresse.
    Par exemple, MEDIUM irait dans les valeurs-sw600dp / ssortingngs.xml et values-medium / ssortingngs.xml et LARGE irait dans sw720dp / ssortingngs.xml et values-large / ssortingngs.xml.
  4. En code, écris
    ScreenSize size = ScreenSize.valueOf(getReources().getSsortingng(R.ssortingng.screensize);

Copiez et collez ce code dans votre Activity et lorsqu’il sera exécuté, il Toast la catégorie de taille d’écran de l’appareil.

 int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; Ssortingng 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(); 

En 2018, si vous avez besoin de la réponse de Jeff à Kotlin, la voici:

  private fun determineScreenSize(): Ssortingng { // Thanks to https://stackoverflow.com/a/5016350/2563009. val screenLayout = resources.configuration.screenLayout return when { screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_SMALL -> "Small" screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_NORMAL -> "Normal" screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_LARGE -> "Large" screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_XLARGE -> "Xlarge" screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_UNDEFINED -> "Undefined" else -> error("Unknown screenLayout: $screenLayout") } } 

Essayez cette fonction isLayoutSizeAtLeast (int screenSize)

Pour vérifier le petit écran, au moins 320×426 dp et au-dessus getResources (). GetConfiguration (). IsLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_SMALL);

Pour vérifier l’écran normal, au moins 320×470 dp et au-dessus getResources (). GetConfiguration (). IsLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_NORMAL);

Pour vérifier le grand écran, au moins 480 x 640 dp et au-dessus getResources (). GetConfiguration (). IsLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_LARGE);

Pour vérifier l’écran extra-large, au moins 720×960 dp et plus getResources (). GetConfiguration (). IsLayoutSizeAtLeast (Configuration.SCREENLAYOUT_SIZE_XLARGE);