getheight() px or dpi?

getheight return height in pixels, Below is what docs says..

  public final int getHeight ()

Since: API Level 1

Return the height of your view. Returns

The height of your view, in pixels.

You need to convert px into dp , use below ways to convert it to dp.

Convert pixel to dp:

public int pxToDp(int px) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return dp;
}

or if you want it in px use below.

Convert dp to pixel:

public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
    return px;
}

It returns pixels. http://developer.android.com/reference/android/view/View.html#getHeight() To convert pixels to dpi use this formula px = dp * (dpi / 160)

Tags:

Android

Dpi