3

I have an emulator with a screen resolution of 720X1280 and a density of 320dpi.

Now, when I use the following code to find out dpheight, dpwidth and density

    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float density  = getResources().getDisplayMetrics().density;
    float dpHeight = outMetrics.heightPixels / density;
    float dpWidth  = outMetrics.widthPixels / density;

I get the following values:

06-24 06:47:46.027 D/Density ( 1003): 2.0
06-24 06:47:46.027 D/DPHeight( 1003): 592.0
06-24 06:47:46.027 D/DPWidth ( 1003): 360.0

I can't make sense of it. Can someone shade some light on it?
I intend to develop an app with loads of text, and I want to set the text sizes appropriately so that it looks consistent.

And yes, I have gone through Google's documentation regarding this.
But I couldn't understand, hence this question.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

4

Density 2.0 is your friend. That's the base dpi (160 dpi = mdpi) multiplier. In facts: 320 / 160 = 2.0

[EDIT]

For clarity:

If your emulator is    then the density is    which actually is  

xxxhdpi                4.0                    640 dpi
 xxhdpi                3.0                    480 dpi
  xhdpi                2.0                    320 dpi
   hdpi                1.5                    240 dpi
   mdpi                1.0                    160 dpi
   ldpi                0.75                   120 dpi
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • So you are saying that, actual dpi of my emulator is 160dpi? Pardon me, if my question seems a bit childish. I need some real clarity on this. –  Jun 24 '15 at 07:19
  • No, it's **320** dpi. In facts, the multiplication scale is **2.0**. If it was 160 dpi, the Density would equal **1.0**: **160 / 160 = 1.0** – Phantômaxx Jun 24 '15 at 07:44
  • Your edited answer is really helpful. So how does the text size work in all scenarios. Lets say I define **small_text=14sp** how do I assign values for this small_text so that it scales well across all devices? –  Jun 24 '15 at 07:56
  • Are dpi values and textsize directly proportional or inversely? –  Jun 24 '15 at 08:02
  • **sp**, as well as **dp** wil be scaled **automatically**. That's why you prefer these units to **px**. There's a relation between px and dp, which uses the density multiplier. and sp are nearly the same as dp - only used for text sizes. – Phantômaxx Jun 24 '15 at 08:09
  • Ok, because on the particular emulator that I mentioned in question (Genymotion) when I changed dpi to 480, all the icons and text seemed to be enlarged, so I figured for higher dpi values I would need to minify the text. –  Jun 24 '15 at 08:30
  • They seem to be enlarged by **1.5x**. I assume `you aren't using the proper density buckets` for your drawable resources. – Phantômaxx Jun 24 '15 at 08:47
  • 1
    I was talking about the normal layout not my app's layout. I am also moving things to proper density buckets. Thanks for your help. –  Jun 24 '15 at 09:26
0

2.0 is the ratio of pixels / dp of your screen. So to get the size of your screen in DP, you need to take it's size in pixels and divide it by your Density (2.0)

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52