How to Get Screen Dimensions as Pixels in Android

How to Get Screen Dimensions as Pixels in Android

22 May 2024 Stephan Petzl Leave a comment Tech-Help

When developing Android applications, there are times when you need to programmatically determine the screen dimensions in pixels. This can be particularly useful for positioning UI elements accurately. In this guide, we will explore various methods to obtain the screen width and height in pixels, ensuring compatibility across different Android versions.

Getting Screen Dimensions in Pixels

To obtain screen dimensions in pixels, you can use several approaches depending on the Android API level your application targets. Below, we discuss the most effective methods.

For API Level 30 and Above

Starting from API level 30, you should use the WindowMetrics.getBounds method. This provides accurate screen dimensions, including the handling of display cutouts and navigation bars.

final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
final WindowInsets windowInsets = metrics.getWindowInsets();
Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout());

int insetsWidth = insets.right + insets.left;
int insetsHeight = + insets.bottom;

final Rect bounds = metrics.getBounds();
final Size legacySize = new Size(bounds.width() - insetsWidth, bounds.height() - insetsHeight);

For API Level 13 to 29

For devices running API level 13 to 29, you can use the Display.getSize(Point) method, which provides the screen dimensions without deprecated methods.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
int width = size.x;
int height = size.y;

For API Level Below 13

For older devices (pre API level 13), you can use the Display.getWidth() and Display.getHeight() methods, although they are deprecated in newer SDKs.

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

Using DisplayMetrics

Another versatile method is using DisplayMetrics, which provides detailed information about the display, including size, density, and font scaling.

DisplayMetrics metrics = new DisplayMetrics();
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
Log.d("ApplicationTagName", "Display width in px is " + widthPixels);

Practical Example

Let’s consider a practical scenario where you need to place a custom element at a specific position relative to the screen edges. Here’s how you can achieve it:

int n = 50; // pixels from the top edge
int m = 30; // pixels from the right edge

// Obtain screen dimensions
DisplayMetrics metrics = new DisplayMetrics();
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;

// Calculate the position
int px = screenWidth - m;
int py = screenHeight - n;

// Set the position of the custom element

Enhance Your Development with Repeato

As a mobile developer, ensuring your app works seamlessly across different devices and screen sizes is crucial. This is where Repeato, our no-code test automation tool for iOS and Android, can significantly streamline your workflow.

Repeato leverages computer vision and AI to create, run, and maintain automated tests for your apps. It’s particularly fast to edit and execute tests, allowing you to focus on creating a great product rather than spending time on testing. Moreover, Repeato enables you to delegate test automation tasks to non-technical colleagues or QAs, ensuring comprehensive testing coverage without additional overhead.

For more details on how Repeato can enhance your mobile app development process, visit our Getting Started guide.

Like this article? there’s more where that came from!