1

The getMeasuredWidth() method measures the width in pixels, is there a similar method that measures the width in dp?

Tenday
  • 11
  • 1

1 Answers1

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