Method One:
public static int screen_width;
public static int screen_height;
Get screen
WindowManager wm = (WindowManager) getbasecontext (). Getsystemservice (
Context.window_service);
Display display = Wm.getdefaultdisplay ();
Point size = new Point ();
Display.getsize (size);
Screen_width= size.x;
Screen_height= size.y;
Display in the GetSize (point) method uses:
Display display = Getwindowmanager (). Getdefaultdisplay ();
int width = display.getwidth (); Deprecated
int height = display.getheight (); Deprecated
View API Discovery:
int getwidth (), int getheight () method: (I'm using level 19)
This method is deprecated in API level 13. Use getSize(Point) instead.
Take a look at the GetSize (point) method:
public void GetSize (point outsize)
Added in API level 13
Gets the size of the display, in pixels.
Note that this value should is used for computing layouts, since a device would typically has screen Decorati On (such as a status bar) along the edges of the display, the amount of application space available from the SI Ze returned here. Layouts should instead use the window size.
The size is adjusted based on the current rotation of the display.
The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display. The returned size is adjusted to exclude certain system decoration elements, that is always visible. It may also is scaled to provide compatibility with older applications, were originally designed for smaller displays
Parameters
| Outsize |
A Point object to receive the size information. |
So how to use it: Scenario 1
: If You want the the display dimensions in pixels you can usegetsize:Display display = Getwindowmanager (). Getdefaultdisplay ();
Point size = new Point ();
Display.getsize (size);
int width = size.x;
int height = size.y; Scenario 2:if you ' re not in a Activity you can get the default Display Viawindow_service:display = ((WindowManager) context.gets Ystemservice (context. Window_service)). Getdefaultdisplay ();
Diswidth = GetContext (). Getresources (). Getdisplaymetrics (). Widthpixels;
Disheight = GetContext (). Getresources (). Getdisplaymetrics (). Heightpixels;
Android gets screen height and width (using the GetSize (point) method in display)