19

Help.I found the height of ListView and I do not know px or dpi? I need dpi

final ListView actualListView = mPullRefreshListView.getRefreshableView();

actualListView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    public void onGlobalLayout() {
                        height = actualListView.getHeight();  

                    }
                });
Max Usanin
  • 2,479
  • 6
  • 40
  • 70
  • getHeight(); always return height in pixels, check this solution for you. http://stackoverflow.com/questions/3166501/getting-the-screen-density-programmatically-in-android – Adeel Pervaiz Aug 08 '12 at 10:21
  • 1
    Just a sidenote for the future. What you need is probably `dp` or `dip` - they stand for *density independent pixels*. `dpi` is *dots per inch* and it measures the density of the screen, not the dimensions. – Marcin Koziński Aug 08 '12 at 10:26

4 Answers4

43

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;
}
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • If used in `Fragment`, `getContext()` can produce `NullPointerException`, instead you should get the context in `onCreateView()`, like this `Context context = view.getContext();` – deyanm Jul 22 '18 at 16:32
  • Does it work for "xdpi" only? What happens if this same code is executed in a mdpi device for example? – Pablo Alfonso Apr 17 '20 at 15:19
2

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)

barisemreefe
  • 434
  • 4
  • 11
1

Using this code you can get runtime Display's Width & Height

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
1

The functions for converting dp to px and px to dp should look like below (in kotlin):

fun convertDpToPx(dp: Int): Int {
    val metrics = Resources.getSystem().displayMetrics
    return dp * (metrics.densityDpi / 160f).roundToInt()
  }

fun convertPxToDp(px: Int): Int {
  val metrics = Resources.getSystem().displayMetrics
  return (px / (metrics.densityDpi / 160f)).roundToInt()
}
mmBs
  • 8,421
  • 6
  • 38
  • 46