Get screen size programatically in Android

So if you ever need to get the screen size(width and height) programatically on Android, note that there are a few options out there. Please see below some of them.

Get Android screen size from DisplayMetrics

The following snippet helps you load the screen size in Android using the Display class. This is quite old, so you might want to look for the other ones.

 * Returns the size of the screen in pixels (width and height)
 * @return a Point that has the screen width stored in x and height in y
public Point getScreenSize(Activity context) {
    Display display = context.getWindowManager().getDefaultDisplay();
    Point size = new Point();

    if (android.os.Build.VERSION.SDK_INT >= 13) {
    } else {
        //noinspection deprecation
        size.set(display.getWidth(), display.getHeight());

    return size;
fun getScreenSize(context: Activity): Point? {
        val display: Display = context.windowManager.defaultDisplay
        val size = Point()
        if (Build.VERSION.SDK_INT >= 13) {
        } else {
            size.set(display.getWidth(), display.getHeight())
        return size

Then, there’s the DisplayMetrics

Using DisplayMetrics is pretty much the same as the one above but the de-structuring looks a bit better.

fun getScreenSize(context: Activity): Pair<Int, Int> {
        val displayMetrics = DisplayMetrics()
        val width = displayMetrics.widthPixels
        val height = displayMetrics.heightPixels
        return (width to height)

At the same time you could get the display metrics from a Resources instance. I guess it could be just a small shortcut if you already have the Resources instance. It looks something like the follwoing:

val displayMetrics = resources.displayMetrics
val width = displayMetrics.widthPixels / displayMetrics.density
val height = displayMetrics.heightPixels / displayMetrics.density

And lastly but newly :P, how to get the screen size in Compose

Getting the screen size in Compose is even easier, you can check it out below. The size of the screen can be found in the Configuration instance like the other approaches but it is easier to get to it.

val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp
val screenWidth = configuration.screenWidthDp.dp

All in all these are some of the methods to get screen size programatically in Android for the regular view system and in Compose.

Next, maybe you want to see more about the Android logs.

Exit mobile version