getheight() px or dpi?

Viewed 24819

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();  

                    }
                });
4 Answers

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()
}
Related