How to Get Screen Width and Height in Android

How to Get Screen Width and Height in Android

6 June 2024 Stephan Petzl Leave a comment Tech-Help

In Android development, determining the screen dimensions of a device is often necessary for various UI adjustments and layouts. This guide will explain how to retrieve the screen width and height using different methods that cater to various Android API levels.

Retrieving Screen Dimensions

Here are the most effective methods to get the screen width and height in Android:

Using DisplayMetrics

The DisplayMetrics class provides a straightforward way to get the screen dimensions. This approach works well across different API levels:

DisplayMetrics displayMetrics = new DisplayMetrics();
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

In a view, you can use:

((Activity) getContext()).getWindowManager()

Including Navigation Bar Height

In some scenarios, you might need to account for the navigation bar height. You can do this by checking if the device has a navigation bar and then adding its height to the screen height:

public boolean showNavigationBar(Resources resources) {
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    return id > 0 && resources.getBoolean(id);

private int getNavigationBarHeight() {
        DisplayMetrics metrics = new DisplayMetrics();
        int usableHeight = metrics.heightPixels;
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight)
            return realHeight - usableHeight;
            return 0;
    return 0;

int height = displayMetrics.heightPixels + getNavigationBarHeight();

Using Resources

A simpler method that doesn’t require a context is to use the system’s resources:

public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;

public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;

Kotlin Extensions

If you are working with Kotlin, you can create extension properties for easier access:

val Context.screenWidth: Int
    get() = resources.displayMetrics.widthPixels

val Context.screenHeight: Int
    get() = resources.displayMetrics.heightPixels

You can then use these properties in your activities or fragments:

val width = context.screenWidth
val height = context.screenHeight

Handling Deprecated Methods

With the introduction of new API levels, some methods have been deprecated. For example, starting from API Level 30, Display.getRealMetrics() and Display.getRealSize() are deprecated. Instead, you can use WindowManager#getCurrentWindowMetrics():

WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics();
Rect bounds = windowMetrics.getBounds();
int width = bounds.width();
int height = bounds.height();


By using the methods described above, you can accurately retrieve the screen dimensions of an Android device. This information is crucial for creating responsive and adaptive user interfaces.

Automating Mobile Testing with Repeato

While developing mobile applications, ensuring the UI works seamlessly across different devices and screen sizes is critical. This is where Repeato, a no-code test automation tool, can be incredibly beneficial. Repeato allows you to create, run, and maintain automated tests for your iOS and Android apps quickly. By leveraging computer vision and AI, it helps you ensure your app’s UI adapts perfectly to various screen dimensions without the need for extensive manual testing.

For more information on how Repeato can streamline your mobile testing processes, visit our documentation.

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