Can anybody explain me what formula android use to calculate screen density?
            Asked
            
        
        
            Active
            
        
            Viewed 2.1k times
        
    4 Answers
40
            Density can be calculated by the following formula:
Density = sqrt((wp * wp) + (hp * hp)) / di
Where:
wp is width resolution in pixels,
hp is height resolution in pixels, and
di is diagonal size in inches.
 
    
    
        C0deH4cker
        
- 3,959
- 1
- 24
- 35
 
    
    
        Hari Kumar
        
- 416
- 4
- 2
- 
                    7This formula actully for [ppi](http://en.wikipedia.org/wiki/Pixel_density) but android likes the term `dpi` instead. Difference can be read [here](http://www.andrewdaceyphotography.com/articles/dpi/) – Fredrick Gauss Oct 14 '13 at 17:01
- 
                    what is diagonal size in inches? – Mahdi Aug 22 '16 at 06:42
- 
                    the distance between two opposite edges of a phone is called diagonal size. – AmiNadimi Sep 17 '16 at 12:48
- 
                    Don't forget to divide bij 160 afterwards – Hibbem Jun 24 '19 at 09:25
1
            
            
        int pixel = 120;
final float scale = getResources().getDisplayMetrics().density;
int dip = (int) (pixel* scale + 0.5f);
Refer this following links
- getDisplayMetrics(), for current display metrics
- density, This is a scaling factor for the Density Independent Pixel unit
 
    
    
        Labeeb Panampullan
        
- 34,521
- 28
- 94
- 112
- 
                    Thanks a lot, but i have to repeat my question again. I want to know how does OS Android calculate density. In your code snippet it is scale value. – teoREtik Feb 09 '11 at 08:45
- 
                    I read a documentation about density, but I didn`t understand it completely. So I want to know, how does it calculate. That`s why I established this question – teoREtik Feb 09 '11 at 09:23
- 
                    2I think you need to rephrase your question. Explain a little more what you want to accomplish. Android's concept of density is the ratio of actual number of pixels/inch to the default pixel density of 160 pixels per inch. Take a look at the [DisplayMetrics](http://developer.android.com/reference/android/util/DisplayMetrics.html) class docs. – Ted Hopp Feb 10 '11 at 04:39
0
            
            
        To calculate screen density, you can use this equation:
Screen density = Screen width (or height) in pixels / Screen width (or height) in inches
 
    
    
        sandes
        
- 1,917
- 17
- 28
 
     
    