How to get screen dimensions as pixels in Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
To achieve pixel-perfect designs or ensure that your app adapts well across different devices, it's essential to get the screen dimensions in pixels on Android. Understanding the screen dimensions helps developers adjust layouts, scale images, and generally make applications responsive. This article explains how you can obtain screen dimensions as pixels in Android using different approaches.
Screen Dimensions in Android
The screen dimensions in Android refer to the width and height in pixels. There are two primary metrics to consider:
- Screen Width: The number of pixels across the width of the display.
- Screen Height: The number of pixels down the height of the display.
Knowing these values is crucial for designing your UI to accommodate small, medium, and large screens appropriately.
Retrieving Screen Dimensions
There are several methods to obtain screen dimensions in Android:
Using DisplayMetrics
The DisplayMetrics class provides a simple way to get the physical dimensions of the screen. It contains various attributes that you can use, like the screen density and size.
Here's how you can use DisplayMetrics to get the screen width and height in pixels:
Using WindowManager
WindowManager is another way to determine screen size. You can retrieve the current display metrics as shown below:
Using Resources Configuration
You can also access the screen dimensions through the Resources class, which may be useful for adjusting resources based on current configuration:
Full-Screen Size with Cutouts
For devices with screen cutouts or notches, you should consider the full screen size using getRealSize instead of getMetrics.
Key Considerations
- Density and DPI: Understanding different screen densities (e.g., mdpi, hdpi, xhdpi) is crucial.
DisplayMetrics.densityDpireturns the screen density. - Orientation: Ensure your app adapts to both portrait and landscape modes, which may require reassessing dimensions at runtime.
- Notch and Cutout: Devices with notches can affect how full-screen dimensions are interpreted.
- API Level: Depending on the API level of the device, some methods for retrieving dimensions may vary in availability and implementation.
Summary Table
| Method | Return Type | Description |
DisplayMetrics | int widthPx, int heightPx | Returns screen dimensions in pixels. |
WindowManager | int widthPx, int heightPx | Retrieves dimensions using window display settings. |
Resources Config | int widthPx, int heightPx | Accesses dimensions through resource configuration. |
getRealSize | int fullWidthPx, int fullHeightPx | Considers full screen dimensions for devices with cutouts. |
Conclusion
Ensure you're always developing with different device specifications in mind, especially given the wide range of Android devices. Use these methods to gather device-specific screen information and make your Android app adaptive and responsive. By understanding and utilizing these approaches, developers can ensure more accurate display scaling and alignment across various screen sizes and densities.

