SoFunction
Updated on 2025-04-09

Android programming method to obtain screen width and height and control width and height

This article describes the method of Android programming to obtain screen width and height and control width and height. Share it for your reference, as follows:

Get screen width and height

// Get the screen width and height (Method 1)int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // Screen width (pixels, such as: 480px)int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // Screen high (pixels, such as: 800p)(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// Get screen density (Method 2)DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = ; // Screen density (pixel ratio: 0.75/1.0/1.5/2.0)int densityDPI = ; // Screen density (pixels per inch: 120/160/240/320)float xdpi = ;
float ydpi = ;
(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
screenWidth = ; // Screen width (pixels, such as: 480px)screenHeight = ; // Screen high (pixels, such as: 800px)(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
// Get screen density (Method 3)dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
density = ; // Screen density (pixel ratio: 0.75/1.0/1.5/2.0)densityDPI = ; // Screen density (pixels per inch: 120/160/240/320)xdpi = ;
ydpi = ;
(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);
(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);
int screenWidthDip = ; // Screen width (dip, such as: 320dip)int screenHeightDip = ; // Screen width (dip, such as: 533dip)(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);
screenWidth = (int)( * density + 0.5f); // Screen width (px, such as: 480px)screenHeight = (int)( * density + 0.5f); // Screen high (px, such as: 800px)(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);

Get the width and height of the control

Generally speaking, the width and height of the control we get in onCreate are all 0. Using the following method, we can get the real width and height.

Method 1:

int w = (0,);
int h = (0,);
(w, h);
int height =();
int width =();
("\n"+height+","+width);

This method will load onMeasure three times

Method 2:

ViewTreeObserver vto = ();
(new () {
  public boolean onPreDraw() {
    int height = ();
    int width = ();
    ("\n"+height+","+width);
    return true;
  }
});

This method will load onMeasure twice, but the callback function will call back many times

Method 3:

ViewTreeObserver vto2 = ();
(new OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
    ().removeGlobalOnLayoutListener(this);
    ("\n\n"+()+","+());
  }
});

This method will load onMeasure twice, but the callback function only calls back once

I hope this article will be helpful to everyone's Android programming design.