Get screen width and height in Android
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Android development, obtaining the screen width and height is often fundamental for creating responsive layouts and ensuring that your application looks great on a variety of devices. Below is a detailed guide on how to retrieve these dimensions using different methods and classes.
Understanding Screen Dimensions
Screen dimensions in Android refer to the width and height of the display in pixels. There are various ways to access these values, depending on the context and the available APIs.
Methods to Obtain Screen Dimensions
- Using
WindowManagerTheWindowManagerclass provides an easy way to access display metrics. Here's how you can use it to get the screen width and height:
In this code snippet, getDefaultDisplay() provides display metrics, which include both width and height in pixels.
- Using
ResourcesClassAlternatively, theResourcesclass can be used to obtain metrics:
This method provides a direct way to access DisplayMetrics using the app context.
- The New
WindowMetricsAPIStarting from Android 11, Google introduced theWindowMetricsAPI. This API offers a more reliable and future-proof way to access window size information:
The WindowMetrics API is part of the new effort to provide more accurate and scenario-based dimensions, accommodating different windowing modes.
Key Considerations
- Screen Orientation: Always consider different screen orientations (portrait and landscape) when calculating dimensions.
- Density Independence: Use density-independent pixels (dp) when defining your layout dimensions for consistency across devices of varying pixel densities.
- Multi-Window Support: For Android N and above, your app could be in multi-window mode. Ensure your logic accounts for dynamic changes in window size.
- Availability of APIs: Consider the minimum API level supported by your app. Newer APIs like
WindowMetricsare not available in older versions, and appropriate fallbacks will be necessary.
Summary Table
| Method | Applicable Android Version | Access Level | Description | Example Code Available |
WindowManager | All | Application | Retrieves screen size directly. | Yes |
Resources | All | Application | Accesses the display metrics through resources. | Yes |
WindowMetrics | Android 11+ | Activity | Provides precise metrics for current windows. | Yes |
Additional Technology Points
- Screen Density AwarenessAndroid devices vary significantly in terms of pixel density. Android uses the concepts of pixels and density-independent pixels (dp) to address this:
- Pixels represent the actual physical screen resolution.
- Density-independent pixels standardize sizing across devices, allowing UI elements to appear consistent in size regardless of device screen density. Utilize the
DisplayMetrics.densityto convert between pixels and dp:
- Responsive DesignWith such variability in Android devices, responsive design becomes critical. Tools such as
ConstraintLayoutandFlexboxLayoutcan help in creating layouts that adapt gracefully to different screen sizes.
In summary, understanding and utilizing the correct methods for obtaining screen dimensions ensures that your app delivers a consistent and user-friendly interface across the wide array of Android devices.

