Android development
screen dimensions
Android tutorial
coding tips
mobile app design

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

  1. Using WindowManager
    The WindowManager class provides an easy way to access display metrics. Here's how you can use it to get the screen width and height:
java
1   import android.content.Context;
2   import android.util.DisplayMetrics;
3   import android.view.WindowManager;
4
5   public class ScreenUtils {
6       public static int[] getScreenDimensions(Context context) {
7           WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
8           DisplayMetrics displayMetrics = new DisplayMetrics();
9           windowManager.getDefaultDisplay().getMetrics(displayMetrics);
10
11           int width = displayMetrics.widthPixels;
12           int height = displayMetrics.heightPixels;
13
14           return new int[]{width, height};
15       }
16   }

In this code snippet, getDefaultDisplay() provides display metrics, which include both width and height in pixels.

  1. Using Resources Class
    Alternatively, the Resources class can be used to obtain metrics:
java
1   import android.content.Context;
2   import android.util.DisplayMetrics;
3
4   public class ScreenUtils {
5       public static int[] getScreenDimensions(Context context) {
6           DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
7
8           int width = displayMetrics.widthPixels;
9           int height = displayMetrics.heightPixels;
10
11           return new int[]{width, height};
12       }
13   }

This method provides a direct way to access DisplayMetrics using the app context.

  1. The New WindowMetrics API
    Starting from Android 11, Google introduced the WindowMetrics API. This API offers a more reliable and future-proof way to access window size information:
java
1   import android.app.Activity;
2   import android.os.Build;
3   import android.util.Size;
4   import android.view.WindowMetrics;
5   import android.view.WindowManager;
6
7   public class ScreenUtils {
8       public static Size getScreenDimensions(Activity activity) {
9           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
10               WindowManager windowManager = activity.getWindowManager();
11               WindowMetrics windowMetrics = windowManager.getCurrentWindowMetrics();
12               return new Size(windowMetrics.getBounds().width(), windowMetrics.getBounds().height());
13           }
14           return null;  // Fallback for older versions
15       }
16   }

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 WindowMetrics are not available in older versions, and appropriate fallbacks will be necessary.

Summary Table

MethodApplicable Android VersionAccess LevelDescriptionExample Code Available
WindowManagerAllApplicationRetrieves screen size directly.Yes
ResourcesAllApplicationAccesses the display metrics through resources.Yes
WindowMetricsAndroid 11+ActivityProvides precise metrics for current windows.Yes

Additional Technology Points

  • Screen Density Awareness
    Android 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.density to convert between pixels and dp:
java
  float density = displayMetrics.density;
  int widthInDp = (int)(widthPixels / density);
  int heightInDp = (int)(heightPixels / density);
  • Responsive Design
    With such variability in Android devices, responsive design becomes critical. Tools such as ConstraintLayout and FlexboxLayout can 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.


Course illustration
Course illustration

All Rights Reserved.