Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the device as LDPI MDPI HDPI or XHDPI [duplicate]

Tags:

Without any code snippet for the application, how to get screen resolution and length of the screen. How could I find whether the device is ldpi, mdpi , hdpi or xhdpi ?

like image 726
Sreeram Avatar asked Feb 25 '13 06:02

Sreeram


People also ask

What is LDPI and Mdpi?

For example, if you have a bitmap drawable that's 48x48 pixels for medium-density screen (the size for a launcher icon), all the different sizes should be: 36x36 for low-density (LDPI) 48x48 for medium-density (MDPI) 72x72 for high-density (HDPI)

What is Mdpi screen?

The normal mdpi is based on a 160 dpi screen, which again is the same as a single pixel unit in your graphics software. Here's a quick breakdown of the dpi measurements for the different pixel densities: ldpi — 120dpi. mdpi — 160 dpi. hdpi — 240 dpi.


1 Answers

Edit: use DisplayMetrics to get the density of the screen

getResources().getDisplayMetrics().densityDpi; 

this will return the int value that represents the following constants. DisplayMetrics.DENSITY_LOW ,DisplayMetrics.DENSITY_MEDIUM, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_XHIGH

  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; } 

This will return following constants based on thsi you can identify the device


Try this

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

Source Identifying screen resolutions

like image 93
Pragnani Avatar answered Sep 22 '22 05:09

Pragnani