How to Check an Android Device Is Hdpi Screen or Mdpi Screen

How to check an Android device is HDPI screen or MDPI screen?

density = getResources().getDisplayMetrics().density;

// return 0.75 if it's LDPI
// return 1.0 if it's MDPI
// return 1.5 if it's HDPI
// return 2.0 if it's XHDPI
// return 3.0 if it's XXHDPI
// return 4.0 if it's XXXHDPI

How to check an Android device is HDPI or MDPI without extending Activity in class?

The problem with window manager is that it needs the activity context, read the name again window manager it manages stuff on the scree, ergo, the activity.

There're some different tricks you can try:
You will need the context for this solution, but it doesn't have to be the activity context, it can be the application context. Actually, to be honest you'll need the resources, which can be accessed through the application context.

on your res folder create a value.xml for the all the parameters you want to find out:

  // res
|- values-hdpi
|- values.xml
|- values-ldpi
|- values.xml
|- values-mdpi
|- values.xml
|- values-xhdpi
|- values.xml

now on each one of those xml you put the following code:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<item name="is_ldpi" format="boolean" type="bool">true</item>
<item name="is_hdpi" format="boolean" type="bool">false</item>
<item name="is_mdpi" format="boolean" type="bool">false</item>
<item name="is_xhdpi" format="boolean" type="bool">false</item>

</resources>

only changing the true or false as appropriate.
now it's just as simple as calling:

  boolean is_hdpi = context.getResources().getBoolean(R.bool.is_hdpi);

how to know which phone support which layout(hdpi , mdpi and xhpi)?

Android treats mdpi (160 pixels/inch) as the base density. So for mdpi devices, 1 dp = 1 pixel. At higher densities, there are more pixels per inch (240 for hdpi, 320 for xhdpi).

AutoMatic Scaling by Android itself:

Android attempts to make graphic images occupy the same physical dimensions on the screen regardless of the device pixel density. So if all it finds is an mdpi resource, and the device is hdpi, it will scale the graphic by 240/160 = 150%, and it will double the size of the graphic for xhdpi.

Using different versions of graphics :

If you don't want this automatic scaling (which can make graphics look poor), you can simply supply your own version of graphic resources for use at higher densities. These graphics should be of the same size that Android would scale an mdpi resource.

Note : the pixels/inch that was stored in the image file has nothing to do with this. It's all based on where you put the graphics files in the resources directory for your project. Any graphics placed in res/drawable are assumed to be properly sized for mdpi displays, as are graphics placed in res/drawable-mdpi. Image files that it finds in res/drawable-hdpi are assumed to be properly sized for hdpi displays, etc. When your program runs on a particular device, Android will first look for a graphic that matches the display density of that device. If it does not find one but instead finds one for a different density, it will use that and automatically scale the image based on the above rules.

As the ldpi, mdpi and hdpi refer to screen density, which means how much pixels can fit into a single inch.

the ratio in pixels between them is:

ldpi = 1:0.75
mdpi = 1:1
hdpi = 1:1.5
xhdpi = 1:2
xxhdpi = 1:3

so lets take an image with about the size of 100X100:

for mdpi it should be 100X100
for ldpi it should be 75X75
for hdpi it should be 150X150
for xhdpi it should be 200X200
for xxhdpi it should be 300X300

this way, for screens with the same size but different DPI, all the images seem the same size on screen.

Call a function based on the device's screen size (hdpi/ldpi/mdpi)

Yeah simply you can check for scale value like this,

final float scale = getResources().getDisplayMetrics().density;

And now you have scale value. The scale value varies like this,

For MDPI devices, scale value is 1.0.

For LDPI devices, scale value is 0.75.

For HDPI devices, scale value is 1.50.

For XHDPI devices, scale value is 2.0.

Just make a cross check,

if(scale <1.50)
{
double height_px = 45 * scale + 0.5;
}

Which means this code will not be executed for High and above resolutions.



Related Topics



Leave a reply



Submit