Get device screen resolution

I used the following method to get the screen size:

public static Point getScreenSize(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    int w = wm.getDefaultDisplay().getWidth();
    int h = wm.getDefaultDisplay().getHeight();
    return new Point(w, h);
}

On my s6 ssung galaxy, this method returns 1080x1920 ... Although my device has a resolution of 1440x2560.

Why? Is there a better way to get a screen resolution that also works on newer phones?

EDIT

I need this method in the service! I have no view / activity for reference, only application context! And I need REAL pixels

+5
source share
6 answers

The best way to get screen resolution is from DisplayMetrics:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
+7
source

API >= 17, getRealSize Display:

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Point size = new Point();
wm.getDefaultDisplay().getRealSize(size);
String resolution = size.x + "x" + size.y;

.

+5

The API returns the number of pixels an application can use, so this is the correct result.

+1
source

Use this:

private static String getScreenResolution(Context context)
{
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    return "{" + width + "," + height + "}";
}
+1
source

Use this.

DisplayMetrics metrics; 
int width = 0, height = 0;

In your onCreate method.

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


 height = metrics.heightPixels;     
 width = metrics.widthPixels;
0
source

Try it:

 Display mDisplay = context.getWindowManager().getDefaultDisplay();

 mDisplay.getWidth();
 mDisplay.getHeight();

Note that this code uses the older API.

0
source

Source: https://habr.com/ru/post/1649291/


All Articles