The getMeasuredWidth() method measures the width in pixels, is there a similar method that measures the width in dp?
            Asked
            
        
        
            Active
            
        
            Viewed 100 times
        
    1
            
            
        - 
                    What for? Everything is drawn in pixels. DP is only there to help you with your layouts – David Medenjak May 12 '17 at 20:37
- 
                    1See: http://stackoverflow.com/questions/4605527/converting-pixels-to-dp – albert c braun May 12 '17 at 20:39
- 
                    No there is no method to get width in dp. But you can use another method to convert pixels to dp. – Ferdous Ahamed May 12 '17 at 20:46
1 Answers
0
            
            
        No, there is no method to get view width in dp. But you can use below method to convert Pixels to DP.
public static float convertPixelsToDp(float px, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return dp;
}
For more clarification see this.
 
    
    
        Community
        
- 1
- 1
 
    
    
        Ferdous Ahamed
        
- 21,438
- 5
- 52
- 61
