I want to know the size of the screen

Asked 2 years ago, Updated 2 years ago, 121 views

n is the pixel at the top of the screen and m is the pixel on the right I'm trying to get the pixel on the top right like the code below. I am asking if there is a way to get screen width and screen height.

    int px = screenWidth - m;
    int py = screenHeight - n;

android layout screen

2022-09-21 21:02

1 Answers

If you want to know the size of the screen, you can use getSize.

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

If it's not an activity

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

You can write it like this. The other way is called Dispaly Metrics, which is a class related to size and density font size

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

If you want to write like this and know the width of the screen

    Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);

You can write it like this.


2022-09-21 21:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.