1.
WindowManager wm = (WindowManager) getcontext () . Getsystemservice (Context.window_service); int width = wm.getdefaultdisplay (). getwidth (); int height = wm.getdefaultdisplay (). GetHeight ();
2.
This . Getwindowmanager (); int width = wm.getdefaultdisplay (). getwidth (); int height = wm.getdefaultdisplay (). GetHeight ();
3. In an activity's OnCreate method, write the following code:
New displaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metric); int width = metric.widthpixels; // screen width (pixels) int height = metric.heightpixels; // screen height (pixels) float density = metric.density; // screen Density (0.75/1.0/1.5) int densitydpi = metric.densitydpi; // screen density dpi (120/160/240)
However, it should be noted that on a low-density small-screen mobile phone, only the above code is not able to get the correct size. For example, a 240x320 pixel low-density mobile phone, if you run the above code, the screen size obtained is 320x427. As a result, the study found that without multi-resolution support, the Android system would convert the 240x320 low-density (120) size to a medium-density (160) dimension, which would greatly affect the coding of the program. Therefore, it is necessary to add the Supports-screens node in the project's Androidmanifest.xml file, the specific content is as follows:
<supports-Screens Android:smallscreens= "true" android:normalscreens= "true" Android:largescreens= "true" android:resizeable= "true" android:anydensity= " True "/>
In this case, the current Android program supports a variety of resolutions, so you can get the correct physical size.
Android gets screen width and height