Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getheight() px or dpi?

Tags:

android

dpi

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

                    }
                });
like image 813
Max Usanin Avatar asked Aug 08 '12 10:08

Max Usanin


People also ask

How do I convert PX to DPI?

There are two options to convert px to dpi: Automatically, using the online converter above to easily and quickly convert pixel to dpi. Manually, using the following formula: DPI = Diagonal In Pixels/Diagonal In Inches

What DPI should I use for photo quality?

In general, photo quality is considered to be a 200 DPI scan at actual size. Consider using a higher scan DPI if you plan to enlarge or reprint an image. Since we are talking about images and image quality, it is important to note how all of this relates to what might be the most well-known form of a pixel – the megapixel.

How do I convert dp units to pixels?

The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities

What is the DPI of 3000x2400 pixels?

To print this image at its highest quality which is at 150 DPI, the image comprised of 3000x2400 pixels must be printed at a size of 3000 px ÷ 150 dpi = 20 inches wide by 2400 px ÷ 150 dpi = 16 inches high. One thing to note for simplicity sake, our examples with 150DPI are for large format printing.


2 Answers

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;
}
like image 196
AAnkit Avatar answered Sep 22 '22 17:09

AAnkit


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)

like image 25
barisemreefe Avatar answered Sep 23 '22 17:09

barisemreefe